blob: 3f77718f2cfa9273ad7302523a43390407caff25 [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
Guido van Rossum98297ee2007-11-06 21:34:58 +000011all_name_chars(Py_UNICODE *s)
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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 if (ok_name_char[*name_chars] == 0) {
17 unsigned char *p;
18 for (p = name_chars; *p; p++)
19 ok_name_char[*p] = 1;
20 }
21 while (*s) {
22 if (*s >= 128)
23 return 0;
24 if (ok_name_char[*s++] == 0)
25 return 0;
26 }
27 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028}
29
30static void
31intern_strings(PyObject *tuple)
32{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
36 PyObject *v = PyTuple_GET_ITEM(tuple, i);
37 if (v == NULL || !PyUnicode_CheckExact(v)) {
38 Py_FatalError("non-string found in code slot");
39 }
40 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
41 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042}
43
44
45PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000046PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 int nlocals, int stacksize, int flags,
48 PyObject *code, PyObject *consts, PyObject *names,
49 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
50 PyObject *filename, PyObject *name, int firstlineno,
51 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 PyCodeObject *co;
Benjamin Peterson90037602011-06-25 22:54:45 -050054 unsigned char *cell2arg = NULL;
55 Py_ssize_t i, n_cellvars;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 /* Check argument types */
58 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
59 code == NULL ||
60 consts == NULL || !PyTuple_Check(consts) ||
61 names == NULL || !PyTuple_Check(names) ||
62 varnames == NULL || !PyTuple_Check(varnames) ||
63 freevars == NULL || !PyTuple_Check(freevars) ||
64 cellvars == NULL || !PyTuple_Check(cellvars) ||
65 name == NULL || !PyUnicode_Check(name) ||
66 filename == NULL || !PyUnicode_Check(filename) ||
67 lnotab == NULL || !PyBytes_Check(lnotab) ||
68 !PyObject_CheckReadBuffer(code)) {
69 PyErr_BadInternalCall();
70 return NULL;
71 }
Benjamin Peterson90037602011-06-25 22:54:45 -050072 n_cellvars = PyTuple_GET_SIZE(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 intern_strings(names);
74 intern_strings(varnames);
75 intern_strings(freevars);
76 intern_strings(cellvars);
77 /* Intern selected string constants */
Benjamin Peterson90037602011-06-25 22:54:45 -050078 for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 PyObject *v = PyTuple_GetItem(consts, i);
80 if (!PyUnicode_Check(v))
81 continue;
82 if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
83 continue;
84 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
85 }
Benjamin Peterson90037602011-06-25 22:54:45 -050086 /* Create mapping between cells and arguments if needed. */
87 if (n_cellvars) {
88 Py_ssize_t total_args = argcount + kwonlyargcount +
89 ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
90 Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
91 int used_cell2arg = 0;
92 cell2arg = PyMem_MALLOC(alloc_size);
93 if (cell2arg == NULL)
94 return NULL;
95 memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
96 /* Find cells which are also arguments. */
97 for (i = 0; i < n_cellvars; i++) {
98 Py_ssize_t j;
99 PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
100 for (j = 0; j < total_args; j++) {
101 PyObject *arg = PyTuple_GET_ITEM(varnames, j);
102 if (!PyUnicode_Compare(cell, arg)) {
103 cell2arg[i] = j;
104 used_cell2arg = 1;
105 break;
106 }
107 }
108 }
109 if (!used_cell2arg) {
110 PyMem_FREE(cell2arg);
111 cell2arg = NULL;
112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 }
Benjamin Peterson90037602011-06-25 22:54:45 -0500114 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
115 if (co == NULL) {
116 if (cell2arg)
117 PyMem_FREE(cell2arg);
118 return NULL;
119 }
120 co->co_argcount = argcount;
121 co->co_kwonlyargcount = kwonlyargcount;
122 co->co_nlocals = nlocals;
123 co->co_stacksize = stacksize;
124 co->co_flags = flags;
125 Py_INCREF(code);
126 co->co_code = code;
127 Py_INCREF(consts);
128 co->co_consts = consts;
129 Py_INCREF(names);
130 co->co_names = names;
131 Py_INCREF(varnames);
132 co->co_varnames = varnames;
133 Py_INCREF(freevars);
134 co->co_freevars = freevars;
135 Py_INCREF(cellvars);
136 co->co_cellvars = cellvars;
137 co->co_cell2arg = cell2arg;
138 Py_INCREF(filename);
139 co->co_filename = filename;
140 Py_INCREF(name);
141 co->co_name = name;
142 co->co_firstlineno = firstlineno;
143 Py_INCREF(lnotab);
144 co->co_lnotab = lnotab;
145 co->co_zombieframe = NULL;
146 co->co_weakreflist = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148}
149
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000150PyCodeObject *
151PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 static PyObject *emptystring = NULL;
154 static PyObject *nulltuple = NULL;
155 PyObject *filename_ob = NULL;
156 PyObject *funcname_ob = NULL;
157 PyCodeObject *result = NULL;
158 if (emptystring == NULL) {
159 emptystring = PyBytes_FromString("");
160 if (emptystring == NULL)
161 goto failed;
162 }
163 if (nulltuple == NULL) {
164 nulltuple = PyTuple_New(0);
165 if (nulltuple == NULL)
166 goto failed;
167 }
168 funcname_ob = PyUnicode_FromString(funcname);
169 if (funcname_ob == NULL)
170 goto failed;
171 filename_ob = PyUnicode_DecodeFSDefault(filename);
172 if (filename_ob == NULL)
173 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 result = PyCode_New(0, /* argcount */
176 0, /* kwonlyargcount */
177 0, /* nlocals */
178 0, /* stacksize */
179 0, /* flags */
180 emptystring, /* code */
181 nulltuple, /* consts */
182 nulltuple, /* names */
183 nulltuple, /* varnames */
184 nulltuple, /* freevars */
185 nulltuple, /* cellvars */
186 filename_ob, /* filename */
187 funcname_ob, /* name */
188 firstlineno, /* firstlineno */
189 emptystring /* lnotab */
190 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000191
192failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 Py_XDECREF(funcname_ob);
194 Py_XDECREF(filename_ob);
195 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000196}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198#define OFF(x) offsetof(PyCodeObject, x)
199
200static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
202 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
203 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
204 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
205 {"co_flags", T_INT, OFF(co_flags), READONLY},
206 {"co_code", T_OBJECT, OFF(co_code), READONLY},
207 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
208 {"co_names", T_OBJECT, OFF(co_names), READONLY},
209 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
210 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
211 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
212 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
213 {"co_name", T_OBJECT, OFF(co_name), READONLY},
214 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
215 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
216 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217};
218
219/* Helper for code_new: return a shallow copy of a tuple that is
220 guaranteed to contain exact strings, by converting string subclasses
221 to exact strings and complaining if a non-string is found. */
222static PyObject*
223validate_and_copy_tuple(PyObject *tup)
224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 PyObject *newtuple;
226 PyObject *item;
227 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 len = PyTuple_GET_SIZE(tup);
230 newtuple = PyTuple_New(len);
231 if (newtuple == NULL)
232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 for (i = 0; i < len; i++) {
235 item = PyTuple_GET_ITEM(tup, i);
236 if (PyUnicode_CheckExact(item)) {
237 Py_INCREF(item);
238 }
239 else if (!PyUnicode_Check(item)) {
240 PyErr_Format(
241 PyExc_TypeError,
242 "name tuples must contain only "
243 "strings, not '%.500s'",
244 item->ob_type->tp_name);
245 Py_DECREF(newtuple);
246 return NULL;
247 }
248 else {
249 item = PyUnicode_FromUnicode(
250 PyUnicode_AS_UNICODE(item),
251 PyUnicode_GET_SIZE(item));
252 if (item == NULL) {
253 Py_DECREF(newtuple);
254 return NULL;
255 }
256 }
257 PyTuple_SET_ITEM(newtuple, i, item);
258 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261}
262
263PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000264"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000265 constants, names, varnames, filename, name, firstlineno,\n\
266 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267\n\
268Create a code object. Not for the faint of heart.");
269
270static PyObject *
271code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 int argcount;
274 int kwonlyargcount;
275 int nlocals;
276 int stacksize;
277 int flags;
278 PyObject *co = NULL;
279 PyObject *code;
280 PyObject *consts;
281 PyObject *names, *ournames = NULL;
282 PyObject *varnames, *ourvarnames = NULL;
283 PyObject *freevars = NULL, *ourfreevars = NULL;
284 PyObject *cellvars = NULL, *ourcellvars = NULL;
285 PyObject *filename;
286 PyObject *name;
287 int firstlineno;
288 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
291 &argcount, &kwonlyargcount,
292 &nlocals, &stacksize, &flags,
293 &code,
294 &PyTuple_Type, &consts,
295 &PyTuple_Type, &names,
296 &PyTuple_Type, &varnames,
297 &filename, &name,
298 &firstlineno, &lnotab,
299 &PyTuple_Type, &freevars,
300 &PyTuple_Type, &cellvars))
301 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (argcount < 0) {
304 PyErr_SetString(
305 PyExc_ValueError,
306 "code: argcount must not be negative");
307 goto cleanup;
308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (kwonlyargcount < 0) {
311 PyErr_SetString(
312 PyExc_ValueError,
313 "code: kwonlyargcount must not be negative");
314 goto cleanup;
315 }
316 if (nlocals < 0) {
317 PyErr_SetString(
318 PyExc_ValueError,
319 "code: nlocals must not be negative");
320 goto cleanup;
321 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 ournames = validate_and_copy_tuple(names);
324 if (ournames == NULL)
325 goto cleanup;
326 ourvarnames = validate_and_copy_tuple(varnames);
327 if (ourvarnames == NULL)
328 goto cleanup;
329 if (freevars)
330 ourfreevars = validate_and_copy_tuple(freevars);
331 else
332 ourfreevars = PyTuple_New(0);
333 if (ourfreevars == NULL)
334 goto cleanup;
335 if (cellvars)
336 ourcellvars = validate_and_copy_tuple(cellvars);
337 else
338 ourcellvars = PyTuple_New(0);
339 if (ourcellvars == NULL)
340 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
343 nlocals, stacksize, flags,
344 code, consts, ournames, ourvarnames,
345 ourfreevars, ourcellvars, filename,
346 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 Py_XDECREF(ournames);
349 Py_XDECREF(ourvarnames);
350 Py_XDECREF(ourfreevars);
351 Py_XDECREF(ourcellvars);
352 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353}
354
355static void
356code_dealloc(PyCodeObject *co)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 Py_XDECREF(co->co_code);
359 Py_XDECREF(co->co_consts);
360 Py_XDECREF(co->co_names);
361 Py_XDECREF(co->co_varnames);
362 Py_XDECREF(co->co_freevars);
363 Py_XDECREF(co->co_cellvars);
364 Py_XDECREF(co->co_filename);
365 Py_XDECREF(co->co_name);
366 Py_XDECREF(co->co_lnotab);
Benjamin Peterson90037602011-06-25 22:54:45 -0500367 if (co->co_cell2arg != NULL)
368 PyMem_FREE(co->co_cell2arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (co->co_zombieframe != NULL)
370 PyObject_GC_Del(co->co_zombieframe);
371 if (co->co_weakreflist != NULL)
372 PyObject_ClearWeakRefs((PyObject*)co);
373 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374}
375
376static PyObject *
377code_repr(PyCodeObject *co)
378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 int lineno;
380 if (co->co_firstlineno != 0)
381 lineno = co->co_firstlineno;
382 else
383 lineno = -1;
384 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
385 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000386 "<code object %U at %p, file \"%U\", line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 co->co_name, co, co->co_filename, lineno);
388 } else {
389 return PyUnicode_FromFormat(
Victor Stinneraaa4e9a2011-01-05 03:33:26 +0000390 "<code object %U at %p, file ???, line %d>",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 co->co_name, co, lineno);
392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393}
394
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000395static PyObject *
396code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 PyCodeObject *co, *cp;
399 int eq;
400 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if ((op != Py_EQ && op != Py_NE) ||
403 !PyCode_Check(self) ||
404 !PyCode_Check(other)) {
Brian Curtindfc80e32011-08-10 20:28:54 -0500405 Py_RETURN_NOTIMPLEMENTED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 co = (PyCodeObject *)self;
409 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
412 if (eq <= 0) goto unequal;
413 eq = co->co_argcount == cp->co_argcount;
414 if (!eq) goto unequal;
415 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
416 if (!eq) goto unequal;
417 eq = co->co_nlocals == cp->co_nlocals;
418 if (!eq) goto unequal;
419 eq = co->co_flags == cp->co_flags;
420 if (!eq) goto unequal;
421 eq = co->co_firstlineno == cp->co_firstlineno;
422 if (!eq) goto unequal;
423 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
424 if (eq <= 0) goto unequal;
425 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
426 if (eq <= 0) goto unequal;
427 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
428 if (eq <= 0) goto unequal;
429 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
430 if (eq <= 0) goto unequal;
431 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
432 if (eq <= 0) goto unequal;
433 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
434 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (op == Py_EQ)
437 res = Py_True;
438 else
439 res = Py_False;
440 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000441
442 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (eq < 0)
444 return NULL;
445 if (op == Py_NE)
446 res = Py_True;
447 else
448 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000449
450 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_INCREF(res);
452 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000455static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456code_hash(PyCodeObject *co)
457{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000458 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 h0 = PyObject_Hash(co->co_name);
460 if (h0 == -1) return -1;
461 h1 = PyObject_Hash(co->co_code);
462 if (h1 == -1) return -1;
463 h2 = PyObject_Hash(co->co_consts);
464 if (h2 == -1) return -1;
465 h3 = PyObject_Hash(co->co_names);
466 if (h3 == -1) return -1;
467 h4 = PyObject_Hash(co->co_varnames);
468 if (h4 == -1) return -1;
469 h5 = PyObject_Hash(co->co_freevars);
470 if (h5 == -1) return -1;
471 h6 = PyObject_Hash(co->co_cellvars);
472 if (h6 == -1) return -1;
473 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
474 co->co_argcount ^ co->co_kwonlyargcount ^
475 co->co_nlocals ^ co->co_flags;
476 if (h == -1) h = -2;
477 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478}
479
480/* XXX code objects need to participate in GC? */
481
482PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyVarObject_HEAD_INIT(&PyType_Type, 0)
484 "code",
485 sizeof(PyCodeObject),
486 0,
487 (destructor)code_dealloc, /* tp_dealloc */
488 0, /* tp_print */
489 0, /* tp_getattr */
490 0, /* tp_setattr */
491 0, /* tp_reserved */
492 (reprfunc)code_repr, /* tp_repr */
493 0, /* tp_as_number */
494 0, /* tp_as_sequence */
495 0, /* tp_as_mapping */
496 (hashfunc)code_hash, /* tp_hash */
497 0, /* tp_call */
498 0, /* tp_str */
499 PyObject_GenericGetAttr, /* tp_getattro */
500 0, /* tp_setattro */
501 0, /* tp_as_buffer */
502 Py_TPFLAGS_DEFAULT, /* tp_flags */
503 code_doc, /* tp_doc */
504 0, /* tp_traverse */
505 0, /* tp_clear */
506 code_richcompare, /* tp_richcompare */
507 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
508 0, /* tp_iter */
509 0, /* tp_iternext */
510 0, /* tp_methods */
511 code_memberlist, /* tp_members */
512 0, /* tp_getset */
513 0, /* tp_base */
514 0, /* tp_dict */
515 0, /* tp_descr_get */
516 0, /* tp_descr_set */
517 0, /* tp_dictoffset */
518 0, /* tp_init */
519 0, /* tp_alloc */
520 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521};
522
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000523/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
524 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525*/
526
527int
528PyCode_Addr2Line(PyCodeObject *co, int addrq)
529{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000530 Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
532 int line = co->co_firstlineno;
533 int addr = 0;
534 while (--size >= 0) {
535 addr += *p++;
536 if (addr > addrq)
537 break;
538 line += *p++;
539 }
540 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000543/* Update *bounds to describe the first and one-past-the-last instructions in
544 the same line as lasti. Return the number of that line. */
545int
546_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000547{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000548 Py_ssize_t size;
549 int addr, line;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
553 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 addr = 0;
556 line = co->co_firstlineno;
557 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* possible optimization: if f->f_lasti == instr_ub
560 (likely to be a common case) then we already know
561 instr_lb -- if we stored the matching value of p
562 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* See lnotab_notes.txt for the description of
565 co_lnotab. A point to remember: increments to p
566 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 bounds->ap_lower = 0;
569 while (size > 0) {
570 if (addr + *p > lasti)
571 break;
572 addr += *p++;
573 if (*p)
574 bounds->ap_lower = addr;
575 line += *p++;
576 --size;
577 }
578
579 if (size > 0) {
580 while (--size >= 0) {
581 addr += *p++;
582 if (*p++)
583 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 bounds->ap_upper = addr;
586 }
587 else {
588 bounds->ap_upper = INT_MAX;
589 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592}