blob: 0489c7b981296a6438bd4867659a71eb9657e174 [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 }
Benjamin Peterson90037602011-06-25 22:54:45 -050077 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 intern_strings(names);
79 intern_strings(varnames);
80 intern_strings(freevars);
81 intern_strings(cellvars);
82 /* Intern selected string constants */
Benjamin Peterson90037602011-06-25 22:54:45 -050083 for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 PyObject *v = PyTuple_GetItem(consts, i);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020085 if (!all_name_chars(v))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 continue;
87 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
88 }
Benjamin Peterson90037602011-06-25 22:54:45 -050089 /* Create mapping between cells and arguments if needed. */
90 if (n_cellvars) {
91 Py_ssize_t total_args = argcount + kwonlyargcount +
92 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
93 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
94 int used_cell2arg = 0;
95 cell2arg = PyMem_MALLOC(alloc_size);
96 if (cell2arg == NULL)
97 return NULL;
98 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
99 /* Find cells which are also arguments. */
100 for (i = 0; i < n_cellvars; i++) {
101 Py_ssize_t j;
102 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
103 for (j = 0; j < total_args; j++) {
104 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
105 if (!PyUnicode_Compare(cell, arg)) {
106 cell2arg[i] = j;
107 used_cell2arg = 1;
108 break;
109 }
110 }
111 }
112 if (!used_cell2arg) {
113 PyMem_FREE(cell2arg);
114 cell2arg = NULL;
115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500117 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
118 if (co == NULL) {
119 if (cell2arg)
120 PyMem_FREE(cell2arg);
121 return NULL;
122 }
123 co->co_argcount = argcount;
124 co->co_kwonlyargcount = kwonlyargcount;
125 co->co_nlocals = nlocals;
126 co->co_stacksize = stacksize;
127 co->co_flags = flags;
128 Py_INCREF(code);
129 co->co_code = code;
130 Py_INCREF(consts);
131 co->co_consts = consts;
132 Py_INCREF(names);
133 co->co_names = names;
134 Py_INCREF(varnames);
135 co->co_varnames = varnames;
136 Py_INCREF(freevars);
137 co->co_freevars = freevars;
138 Py_INCREF(cellvars);
139 co->co_cellvars = cellvars;
140 co->co_cell2arg = cell2arg;
141 Py_INCREF(filename);
142 co->co_filename = filename;
143 Py_INCREF(name);
144 co->co_name = name;
145 co->co_firstlineno = firstlineno;
146 Py_INCREF(lnotab);
147 co->co_lnotab = lnotab;
148 co->co_zombieframe = NULL;
149 co->co_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151}
152
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000153PyCodeObject *
154PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 static PyObject *emptystring = NULL;
157 static PyObject *nulltuple = NULL;
158 PyObject *filename_ob = NULL;
159 PyObject *funcname_ob = NULL;
160 PyCodeObject *result = NULL;
161 if (emptystring == NULL) {
162 emptystring = PyBytes_FromString("");
163 if (emptystring == NULL)
164 goto failed;
165 }
166 if (nulltuple == NULL) {
167 nulltuple = PyTuple_New(0);
168 if (nulltuple == NULL)
169 goto failed;
170 }
171 funcname_ob = PyUnicode_FromString(funcname);
172 if (funcname_ob == NULL)
173 goto failed;
174 filename_ob = PyUnicode_DecodeFSDefault(filename);
175 if (filename_ob == NULL)
176 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 result = PyCode_New(0, /* argcount */
179 0, /* kwonlyargcount */
180 0, /* nlocals */
181 0, /* stacksize */
182 0, /* flags */
183 emptystring, /* code */
184 nulltuple, /* consts */
185 nulltuple, /* names */
186 nulltuple, /* varnames */
187 nulltuple, /* freevars */
188 nulltuple, /* cellvars */
189 filename_ob, /* filename */
190 funcname_ob, /* name */
191 firstlineno, /* firstlineno */
192 emptystring /* lnotab */
193 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000194
195failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 Py_XDECREF(funcname_ob);
197 Py_XDECREF(filename_ob);
198 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000199}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
201#define OFF(x) offsetof(PyCodeObject, x)
202
203static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
205 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
206 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
207 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
208 {"co_flags", T_INT, OFF(co_flags), READONLY},
209 {"co_code", T_OBJECT, OFF(co_code), READONLY},
210 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
211 {"co_names", T_OBJECT, OFF(co_names), READONLY},
212 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
213 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
214 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
215 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
216 {"co_name", T_OBJECT, OFF(co_name), READONLY},
217 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
218 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
219 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220};
221
222/* Helper for code_new: return a shallow copy of a tuple that is
223 guaranteed to contain exact strings, by converting string subclasses
224 to exact strings and complaining if a non-string is found. */
225static PyObject*
226validate_and_copy_tuple(PyObject *tup)
227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 PyObject *newtuple;
229 PyObject *item;
230 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 len = PyTuple_GET_SIZE(tup);
233 newtuple = PyTuple_New(len);
234 if (newtuple == NULL)
235 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 for (i = 0; i < len; i++) {
238 item = PyTuple_GET_ITEM(tup, i);
239 if (PyUnicode_CheckExact(item)) {
240 Py_INCREF(item);
241 }
242 else if (!PyUnicode_Check(item)) {
243 PyErr_Format(
244 PyExc_TypeError,
245 "name tuples must contain only "
246 "strings, not '%.500s'",
247 item->ob_type->tp_name);
248 Py_DECREF(newtuple);
249 return NULL;
250 }
251 else {
252 item = PyUnicode_FromUnicode(
253 PyUnicode_AS_UNICODE(item),
254 PyUnicode_GET_SIZE(item));
255 if (item == NULL) {
256 Py_DECREF(newtuple);
257 return NULL;
258 }
259 }
260 PyTuple_SET_ITEM(newtuple, i, item);
261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264}
265
266PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000267"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000268 constants, names, varnames, filename, name, firstlineno,\n\
269 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270\n\
271Create a code object. Not for the faint of heart.");
272
273static PyObject *
274code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 int argcount;
277 int kwonlyargcount;
278 int nlocals;
279 int stacksize;
280 int flags;
281 PyObject *co = NULL;
282 PyObject *code;
283 PyObject *consts;
284 PyObject *names, *ournames = NULL;
285 PyObject *varnames, *ourvarnames = NULL;
286 PyObject *freevars = NULL, *ourfreevars = NULL;
287 PyObject *cellvars = NULL, *ourcellvars = NULL;
288 PyObject *filename;
289 PyObject *name;
290 int firstlineno;
291 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
294 &argcount, &kwonlyargcount,
295 &nlocals, &stacksize, &flags,
296 &code,
297 &PyTuple_Type, &consts,
298 &PyTuple_Type, &names,
299 &PyTuple_Type, &varnames,
300 &filename, &name,
301 &firstlineno, &lnotab,
302 &PyTuple_Type, &freevars,
303 &PyTuple_Type, &cellvars))
304 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (argcount < 0) {
307 PyErr_SetString(
308 PyExc_ValueError,
309 "code: argcount must not be negative");
310 goto cleanup;
311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (kwonlyargcount < 0) {
314 PyErr_SetString(
315 PyExc_ValueError,
316 "code: kwonlyargcount must not be negative");
317 goto cleanup;
318 }
319 if (nlocals < 0) {
320 PyErr_SetString(
321 PyExc_ValueError,
322 "code: nlocals must not be negative");
323 goto cleanup;
324 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 ournames = validate_and_copy_tuple(names);
327 if (ournames == NULL)
328 goto cleanup;
329 ourvarnames = validate_and_copy_tuple(varnames);
330 if (ourvarnames == NULL)
331 goto cleanup;
332 if (freevars)
333 ourfreevars = validate_and_copy_tuple(freevars);
334 else
335 ourfreevars = PyTuple_New(0);
336 if (ourfreevars == NULL)
337 goto cleanup;
338 if (cellvars)
339 ourcellvars = validate_and_copy_tuple(cellvars);
340 else
341 ourcellvars = PyTuple_New(0);
342 if (ourcellvars == NULL)
343 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
346 nlocals, stacksize, flags,
347 code, consts, ournames, ourvarnames,
348 ourfreevars, ourcellvars, filename,
349 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 Py_XDECREF(ournames);
352 Py_XDECREF(ourvarnames);
353 Py_XDECREF(ourfreevars);
354 Py_XDECREF(ourcellvars);
355 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356}
357
358static void
359code_dealloc(PyCodeObject *co)
360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_XDECREF(co->co_code);
362 Py_XDECREF(co->co_consts);
363 Py_XDECREF(co->co_names);
364 Py_XDECREF(co->co_varnames);
365 Py_XDECREF(co->co_freevars);
366 Py_XDECREF(co->co_cellvars);
367 Py_XDECREF(co->co_filename);
368 Py_XDECREF(co->co_name);
369 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500370 if (co->co_cell2arg != NULL)
371 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (co->co_zombieframe != NULL)
373 PyObject_GC_Del(co->co_zombieframe);
374 if (co->co_weakreflist != NULL)
375 PyObject_ClearWeakRefs((PyObject*)co);
376 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377}
378
379static PyObject *
380code_repr(PyCodeObject *co)
381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 int lineno;
383 if (co->co_firstlineno != 0)
384 lineno = co->co_firstlineno;
385 else
386 lineno = -1;
387 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
388 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000389 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 co->co_name, co, co->co_filename, lineno);
391 } else {
392 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000393 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 co->co_name, co, lineno);
395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396}
397
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000398static PyObject *
399code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 PyCodeObject *co, *cp;
402 int eq;
403 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if ((op != Py_EQ && op != Py_NE) ||
406 !PyCode_Check(self) ||
407 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500408 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 co = (PyCodeObject *)self;
412 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
415 if (eq <= 0) goto unequal;
416 eq = co->co_argcount == cp->co_argcount;
417 if (!eq) goto unequal;
418 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
419 if (!eq) goto unequal;
420 eq = co->co_nlocals == cp->co_nlocals;
421 if (!eq) goto unequal;
422 eq = co->co_flags == cp->co_flags;
423 if (!eq) goto unequal;
424 eq = co->co_firstlineno == cp->co_firstlineno;
425 if (!eq) goto unequal;
426 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
427 if (eq <= 0) goto unequal;
428 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
429 if (eq <= 0) goto unequal;
430 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
431 if (eq <= 0) goto unequal;
432 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
433 if (eq <= 0) goto unequal;
434 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
435 if (eq <= 0) goto unequal;
436 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
437 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (op == Py_EQ)
440 res = Py_True;
441 else
442 res = Py_False;
443 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000444
445 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (eq < 0)
447 return NULL;
448 if (op == Py_NE)
449 res = Py_True;
450 else
451 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000452
453 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 Py_INCREF(res);
455 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456}
457
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000458static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459code_hash(PyCodeObject *co)
460{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000461 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 h0 = PyObject_Hash(co->co_name);
463 if (h0 == -1) return -1;
464 h1 = PyObject_Hash(co->co_code);
465 if (h1 == -1) return -1;
466 h2 = PyObject_Hash(co->co_consts);
467 if (h2 == -1) return -1;
468 h3 = PyObject_Hash(co->co_names);
469 if (h3 == -1) return -1;
470 h4 = PyObject_Hash(co->co_varnames);
471 if (h4 == -1) return -1;
472 h5 = PyObject_Hash(co->co_freevars);
473 if (h5 == -1) return -1;
474 h6 = PyObject_Hash(co->co_cellvars);
475 if (h6 == -1) return -1;
476 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
477 co->co_argcount ^ co->co_kwonlyargcount ^
478 co->co_nlocals ^ co->co_flags;
479 if (h == -1) h = -2;
480 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481}
482
483/* XXX code objects need to participate in GC? */
484
485PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyVarObject_HEAD_INIT(&PyType_Type, 0)
487 "code",
488 sizeof(PyCodeObject),
489 0,
490 (destructor)code_dealloc, /* tp_dealloc */
491 0, /* tp_print */
492 0, /* tp_getattr */
493 0, /* tp_setattr */
494 0, /* tp_reserved */
495 (reprfunc)code_repr, /* tp_repr */
496 0, /* tp_as_number */
497 0, /* tp_as_sequence */
498 0, /* tp_as_mapping */
499 (hashfunc)code_hash, /* tp_hash */
500 0, /* tp_call */
501 0, /* tp_str */
502 PyObject_GenericGetAttr, /* tp_getattro */
503 0, /* tp_setattro */
504 0, /* tp_as_buffer */
505 Py_TPFLAGS_DEFAULT, /* tp_flags */
506 code_doc, /* tp_doc */
507 0, /* tp_traverse */
508 0, /* tp_clear */
509 code_richcompare, /* tp_richcompare */
510 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
511 0, /* tp_iter */
512 0, /* tp_iternext */
513 0, /* tp_methods */
514 code_memberlist, /* tp_members */
515 0, /* tp_getset */
516 0, /* tp_base */
517 0, /* tp_dict */
518 0, /* tp_descr_get */
519 0, /* tp_descr_set */
520 0, /* tp_dictoffset */
521 0, /* tp_init */
522 0, /* tp_alloc */
523 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524};
525
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000526/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
527 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528*/
529
530int
531PyCode_Addr2Line(PyCodeObject *co, int addrq)
532{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000533 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
535 int line = co->co_firstlineno;
536 int addr = 0;
537 while (--size >= 0) {
538 addr += *p++;
539 if (addr > addrq)
540 break;
541 line += *p++;
542 }
543 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000546/* Update *bounds to describe the first and one-past-the-last instructions in
547 the same line as lasti. Return the number of that line. */
548int
549_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000551 Py_ssize_t size;
552 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
556 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 addr = 0;
559 line = co->co_firstlineno;
560 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* possible optimization: if f->f_lasti == instr_ub
563 (likely to be a common case) then we already know
564 instr_lb -- if we stored the matching value of p
565 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* See lnotab_notes.txt for the description of
568 co_lnotab. A point to remember: increments to p
569 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 bounds->ap_lower = 0;
572 while (size > 0) {
573 if (addr + *p > lasti)
574 break;
575 addr += *p++;
576 if (*p)
577 bounds->ap_lower = addr;
578 line += *p++;
579 --size;
580 }
581
582 if (size > 0) {
583 while (--size >= 0) {
584 addr += *p++;
585 if (*p++)
586 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 bounds->ap_upper = addr;
589 }
590 else {
591 bounds->ap_upper = INT_MAX;
592 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595}