blob: 353f414a38486b8ab319d8fdd07934ba5b4b54d5 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001#include "Python.h"
2#include "code.h"
3#include "structmember.h"
4
5#define NAME_CHARS \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007
8/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
9
10static int
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020011all_name_chars(PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000013 static char ok_name_char[256];
14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020015 PyUnicodeObject *u = (PyUnicodeObject *)o;
16 const unsigned char *s;
17
18 if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
19 PyUnicode_MAX_CHAR_VALUE(u) >= 128)
20 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 if (ok_name_char[*name_chars] == 0) {
23 unsigned char *p;
24 for (p = name_chars; *p; p++)
25 ok_name_char[*p] = 1;
26 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020027 s = PyUnicode_1BYTE_DATA(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 while (*s) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 if (ok_name_char[*s++] == 0)
30 return 0;
31 }
32 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033}
34
35static void
36intern_strings(PyObject *tuple)
37{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
41 PyObject *v = PyTuple_GET_ITEM(tuple, i);
42 if (v == NULL || !PyUnicode_CheckExact(v)) {
43 Py_FatalError("non-string found in code slot");
44 }
45 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
46 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047}
48
49
50PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000051PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 int nlocals, int stacksize, int flags,
53 PyObject *code, PyObject *consts, PyObject *names,
54 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
55 PyObject *filename, PyObject *name, int firstlineno,
56 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 PyCodeObject *co;
Benjamin Peterson90037602011-06-25 22:54:45 -050059 unsigned char *cell2arg = NULL;
60 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 /* Check argument types */
63 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
64 code == NULL ||
65 consts == NULL || !PyTuple_Check(consts) ||
66 names == NULL || !PyTuple_Check(names) ||
67 varnames == NULL || !PyTuple_Check(varnames) ||
68 freevars == NULL || !PyTuple_Check(freevars) ||
69 cellvars == NULL || !PyTuple_Check(cellvars) ||
70 name == NULL || !PyUnicode_Check(name) ||
71 filename == NULL || !PyUnicode_Check(filename) ||
72 lnotab == NULL || !PyBytes_Check(lnotab) ||
73 !PyObject_CheckReadBuffer(code)) {
74 PyErr_BadInternalCall();
75 return NULL;
76 }
Victor Stinner7c74de42013-10-10 15:55:14 +020077
78 /* Ensure that the filename is a ready Unicode string */
79 if (PyUnicode_READY(filename) < 0)
80 return NULL;
81
Benjamin Peterson90037602011-06-25 22:54:45 -050082 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 intern_strings(names);
84 intern_strings(varnames);
85 intern_strings(freevars);
86 intern_strings(cellvars);
87 /* Intern selected string constants */
Benjamin Peterson90037602011-06-25 22:54:45 -050088 for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 PyObject *v = PyTuple_GetItem(consts, i);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020090 if (!all_name_chars(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 continue;
92 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
93 }
Benjamin Peterson90037602011-06-25 22:54:45 -050094 /* Create mapping between cells and arguments if needed. */
95 if (n_cellvars) {
96 Py_ssize_t total_args = argcount + kwonlyargcount +
97 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
98 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
99 int used_cell2arg = 0;
100 cell2arg = PyMem_MALLOC(alloc_size);
101 if (cell2arg == NULL)
102 return NULL;
103 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
104 /* Find cells which are also arguments. */
105 for (i = 0; i < n_cellvars; i++) {
106 Py_ssize_t j;
107 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
108 for (j = 0; j < total_args; j++) {
109 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
110 if (!PyUnicode_Compare(cell, arg)) {
111 cell2arg[i] = j;
112 used_cell2arg = 1;
113 break;
114 }
115 }
116 }
117 if (!used_cell2arg) {
118 PyMem_FREE(cell2arg);
119 cell2arg = NULL;
120 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500122 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
123 if (co == NULL) {
124 if (cell2arg)
125 PyMem_FREE(cell2arg);
126 return NULL;
127 }
128 co->co_argcount = argcount;
129 co->co_kwonlyargcount = kwonlyargcount;
130 co->co_nlocals = nlocals;
131 co->co_stacksize = stacksize;
132 co->co_flags = flags;
133 Py_INCREF(code);
134 co->co_code = code;
135 Py_INCREF(consts);
136 co->co_consts = consts;
137 Py_INCREF(names);
138 co->co_names = names;
139 Py_INCREF(varnames);
140 co->co_varnames = varnames;
141 Py_INCREF(freevars);
142 co->co_freevars = freevars;
143 Py_INCREF(cellvars);
144 co->co_cellvars = cellvars;
145 co->co_cell2arg = cell2arg;
146 Py_INCREF(filename);
147 co->co_filename = filename;
148 Py_INCREF(name);
149 co->co_name = name;
150 co->co_firstlineno = firstlineno;
151 Py_INCREF(lnotab);
152 co->co_lnotab = lnotab;
153 co->co_zombieframe = NULL;
154 co->co_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156}
157
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000158PyCodeObject *
159PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 static PyObject *emptystring = NULL;
162 static PyObject *nulltuple = NULL;
163 PyObject *filename_ob = NULL;
164 PyObject *funcname_ob = NULL;
165 PyCodeObject *result = NULL;
166 if (emptystring == NULL) {
167 emptystring = PyBytes_FromString("");
168 if (emptystring == NULL)
169 goto failed;
170 }
171 if (nulltuple == NULL) {
172 nulltuple = PyTuple_New(0);
173 if (nulltuple == NULL)
174 goto failed;
175 }
176 funcname_ob = PyUnicode_FromString(funcname);
177 if (funcname_ob == NULL)
178 goto failed;
179 filename_ob = PyUnicode_DecodeFSDefault(filename);
180 if (filename_ob == NULL)
181 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 result = PyCode_New(0, /* argcount */
184 0, /* kwonlyargcount */
185 0, /* nlocals */
186 0, /* stacksize */
187 0, /* flags */
188 emptystring, /* code */
189 nulltuple, /* consts */
190 nulltuple, /* names */
191 nulltuple, /* varnames */
192 nulltuple, /* freevars */
193 nulltuple, /* cellvars */
194 filename_ob, /* filename */
195 funcname_ob, /* name */
196 firstlineno, /* firstlineno */
197 emptystring /* lnotab */
198 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000199
200failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 Py_XDECREF(funcname_ob);
202 Py_XDECREF(filename_ob);
203 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000204}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
206#define OFF(x) offsetof(PyCodeObject, x)
207
208static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
210 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
211 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
212 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
213 {"co_flags", T_INT, OFF(co_flags), READONLY},
214 {"co_code", T_OBJECT, OFF(co_code), READONLY},
215 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
216 {"co_names", T_OBJECT, OFF(co_names), READONLY},
217 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
218 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
219 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
220 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
221 {"co_name", T_OBJECT, OFF(co_name), READONLY},
222 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
223 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
224 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225};
226
227/* Helper for code_new: return a shallow copy of a tuple that is
228 guaranteed to contain exact strings, by converting string subclasses
229 to exact strings and complaining if a non-string is found. */
230static PyObject*
231validate_and_copy_tuple(PyObject *tup)
232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PyObject *newtuple;
234 PyObject *item;
235 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 len = PyTuple_GET_SIZE(tup);
238 newtuple = PyTuple_New(len);
239 if (newtuple == NULL)
240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 for (i = 0; i < len; i++) {
243 item = PyTuple_GET_ITEM(tup, i);
244 if (PyUnicode_CheckExact(item)) {
245 Py_INCREF(item);
246 }
247 else if (!PyUnicode_Check(item)) {
248 PyErr_Format(
249 PyExc_TypeError,
250 "name tuples must contain only "
251 "strings, not '%.500s'",
252 item->ob_type->tp_name);
253 Py_DECREF(newtuple);
254 return NULL;
255 }
256 else {
Victor Stinnerbf6e5602011-12-12 01:53:47 +0100257 item = _PyUnicode_Copy(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (item == NULL) {
259 Py_DECREF(newtuple);
260 return NULL;
261 }
262 }
263 PyTuple_SET_ITEM(newtuple, i, item);
264 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267}
268
269PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000270"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000271 constants, names, varnames, filename, name, firstlineno,\n\
272 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273\n\
274Create a code object. Not for the faint of heart.");
275
276static PyObject *
277code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 int argcount;
280 int kwonlyargcount;
281 int nlocals;
282 int stacksize;
283 int flags;
284 PyObject *co = NULL;
285 PyObject *code;
286 PyObject *consts;
287 PyObject *names, *ournames = NULL;
288 PyObject *varnames, *ourvarnames = NULL;
289 PyObject *freevars = NULL, *ourfreevars = NULL;
290 PyObject *cellvars = NULL, *ourcellvars = NULL;
291 PyObject *filename;
292 PyObject *name;
293 int firstlineno;
294 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
297 &argcount, &kwonlyargcount,
298 &nlocals, &stacksize, &flags,
299 &code,
300 &PyTuple_Type, &consts,
301 &PyTuple_Type, &names,
302 &PyTuple_Type, &varnames,
303 &filename, &name,
304 &firstlineno, &lnotab,
305 &PyTuple_Type, &freevars,
306 &PyTuple_Type, &cellvars))
307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (argcount < 0) {
310 PyErr_SetString(
311 PyExc_ValueError,
312 "code: argcount must not be negative");
313 goto cleanup;
314 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (kwonlyargcount < 0) {
317 PyErr_SetString(
318 PyExc_ValueError,
319 "code: kwonlyargcount must not be negative");
320 goto cleanup;
321 }
322 if (nlocals < 0) {
323 PyErr_SetString(
324 PyExc_ValueError,
325 "code: nlocals must not be negative");
326 goto cleanup;
327 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 ournames = validate_and_copy_tuple(names);
330 if (ournames == NULL)
331 goto cleanup;
332 ourvarnames = validate_and_copy_tuple(varnames);
333 if (ourvarnames == NULL)
334 goto cleanup;
335 if (freevars)
336 ourfreevars = validate_and_copy_tuple(freevars);
337 else
338 ourfreevars = PyTuple_New(0);
339 if (ourfreevars == NULL)
340 goto cleanup;
341 if (cellvars)
342 ourcellvars = validate_and_copy_tuple(cellvars);
343 else
344 ourcellvars = PyTuple_New(0);
345 if (ourcellvars == NULL)
346 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
349 nlocals, stacksize, flags,
350 code, consts, ournames, ourvarnames,
351 ourfreevars, ourcellvars, filename,
352 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 Py_XDECREF(ournames);
355 Py_XDECREF(ourvarnames);
356 Py_XDECREF(ourfreevars);
357 Py_XDECREF(ourcellvars);
358 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359}
360
361static void
362code_dealloc(PyCodeObject *co)
363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 Py_XDECREF(co->co_code);
365 Py_XDECREF(co->co_consts);
366 Py_XDECREF(co->co_names);
367 Py_XDECREF(co->co_varnames);
368 Py_XDECREF(co->co_freevars);
369 Py_XDECREF(co->co_cellvars);
370 Py_XDECREF(co->co_filename);
371 Py_XDECREF(co->co_name);
372 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500373 if (co->co_cell2arg != NULL)
374 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 if (co->co_zombieframe != NULL)
376 PyObject_GC_Del(co->co_zombieframe);
377 if (co->co_weakreflist != NULL)
378 PyObject_ClearWeakRefs((PyObject*)co);
379 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380}
381
382static PyObject *
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200383code_sizeof(PyCodeObject *co, void *unused)
384{
385 Py_ssize_t res;
386
387 res = sizeof(PyCodeObject);
388 if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
389 res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
390 return PyLong_FromSsize_t(res);
391}
392
393static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394code_repr(PyCodeObject *co)
395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 int lineno;
397 if (co->co_firstlineno != 0)
398 lineno = co->co_firstlineno;
399 else
400 lineno = -1;
401 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
402 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000403 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 co->co_name, co, co->co_filename, lineno);
405 } else {
406 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000407 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 co->co_name, co, lineno);
409 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410}
411
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000412static PyObject *
413code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 PyCodeObject *co, *cp;
416 int eq;
417 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if ((op != Py_EQ && op != Py_NE) ||
420 !PyCode_Check(self) ||
421 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500422 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 co = (PyCodeObject *)self;
426 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
429 if (eq <= 0) goto unequal;
430 eq = co->co_argcount == cp->co_argcount;
431 if (!eq) goto unequal;
432 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
433 if (!eq) goto unequal;
434 eq = co->co_nlocals == cp->co_nlocals;
435 if (!eq) goto unequal;
436 eq = co->co_flags == cp->co_flags;
437 if (!eq) goto unequal;
438 eq = co->co_firstlineno == cp->co_firstlineno;
439 if (!eq) goto unequal;
440 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
441 if (eq <= 0) goto unequal;
442 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
443 if (eq <= 0) goto unequal;
444 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
445 if (eq <= 0) goto unequal;
446 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
447 if (eq <= 0) goto unequal;
448 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
449 if (eq <= 0) goto unequal;
450 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
451 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (op == Py_EQ)
454 res = Py_True;
455 else
456 res = Py_False;
457 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000458
459 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 if (eq < 0)
461 return NULL;
462 if (op == Py_NE)
463 res = Py_True;
464 else
465 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000466
467 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 Py_INCREF(res);
469 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470}
471
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000472static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473code_hash(PyCodeObject *co)
474{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000475 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 h0 = PyObject_Hash(co->co_name);
477 if (h0 == -1) return -1;
478 h1 = PyObject_Hash(co->co_code);
479 if (h1 == -1) return -1;
480 h2 = PyObject_Hash(co->co_consts);
481 if (h2 == -1) return -1;
482 h3 = PyObject_Hash(co->co_names);
483 if (h3 == -1) return -1;
484 h4 = PyObject_Hash(co->co_varnames);
485 if (h4 == -1) return -1;
486 h5 = PyObject_Hash(co->co_freevars);
487 if (h5 == -1) return -1;
488 h6 = PyObject_Hash(co->co_cellvars);
489 if (h6 == -1) return -1;
490 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
491 co->co_argcount ^ co->co_kwonlyargcount ^
492 co->co_nlocals ^ co->co_flags;
493 if (h == -1) h = -2;
494 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495}
496
497/* XXX code objects need to participate in GC? */
498
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200499static struct PyMethodDef code_methods[] = {
500 {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
501 {NULL, NULL} /* sentinel */
502};
503
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 PyVarObject_HEAD_INIT(&PyType_Type, 0)
506 "code",
507 sizeof(PyCodeObject),
508 0,
509 (destructor)code_dealloc, /* tp_dealloc */
510 0, /* tp_print */
511 0, /* tp_getattr */
512 0, /* tp_setattr */
513 0, /* tp_reserved */
514 (reprfunc)code_repr, /* tp_repr */
515 0, /* tp_as_number */
516 0, /* tp_as_sequence */
517 0, /* tp_as_mapping */
518 (hashfunc)code_hash, /* tp_hash */
519 0, /* tp_call */
520 0, /* tp_str */
521 PyObject_GenericGetAttr, /* tp_getattro */
522 0, /* tp_setattro */
523 0, /* tp_as_buffer */
524 Py_TPFLAGS_DEFAULT, /* tp_flags */
525 code_doc, /* tp_doc */
526 0, /* tp_traverse */
527 0, /* tp_clear */
528 code_richcompare, /* tp_richcompare */
529 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
530 0, /* tp_iter */
531 0, /* tp_iternext */
Martin v. Löwis3bbd2fa2012-07-26 22:23:23 +0200532 code_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 code_memberlist, /* tp_members */
534 0, /* tp_getset */
535 0, /* tp_base */
536 0, /* tp_dict */
537 0, /* tp_descr_get */
538 0, /* tp_descr_set */
539 0, /* tp_dictoffset */
540 0, /* tp_init */
541 0, /* tp_alloc */
542 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543};
544
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000545/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
546 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547*/
548
549int
550PyCode_Addr2Line(PyCodeObject *co, int addrq)
551{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000552 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
554 int line = co->co_firstlineno;
555 int addr = 0;
556 while (--size >= 0) {
557 addr += *p++;
558 if (addr > addrq)
559 break;
560 line += *p++;
561 }
562 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000564
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000565/* Update *bounds to describe the first and one-past-the-last instructions in
566 the same line as lasti. Return the number of that line. */
567int
568_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000570 Py_ssize_t size;
571 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
575 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 addr = 0;
578 line = co->co_firstlineno;
579 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* possible optimization: if f->f_lasti == instr_ub
582 (likely to be a common case) then we already know
583 instr_lb -- if we stored the matching value of p
584 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 /* See lnotab_notes.txt for the description of
587 co_lnotab. A point to remember: increments to p
588 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 bounds->ap_lower = 0;
591 while (size > 0) {
592 if (addr + *p > lasti)
593 break;
594 addr += *p++;
595 if (*p)
596 bounds->ap_lower = addr;
597 line += *p++;
598 --size;
599 }
600
601 if (size > 0) {
602 while (--size >= 0) {
603 addr += *p++;
604 if (*p++)
605 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 bounds->ap_upper = addr;
608 }
609 else {
610 bounds->ap_upper = INT_MAX;
611 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614}