blob: 6d5daa82dccd8e70a53f2c242c13d4426590e949 [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 \
6 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
7
8/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
9
10static int
11all_name_chars(unsigned char *s)
12{
13 static char ok_name_char[256];
14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15
16 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 (ok_name_char[*s++] == 0)
23 return 0;
24 }
25 return 1;
26}
27
28static void
29intern_strings(PyObject *tuple)
30{
Martin v. Löwis18e16552006-02-15 17:27:45 +000031 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032
33 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
34 PyObject *v = PyTuple_GET_ITEM(tuple, i);
35 if (v == NULL || !PyString_CheckExact(v)) {
36 Py_FatalError("non-string found in code slot");
37 }
38 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
39 }
40}
41
42
43PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000044PyCode_New(int argcount, int kwonlyargcount,
45 int nlocals, int stacksize, int flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046 PyObject *code, PyObject *consts, PyObject *names,
47 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
48 PyObject *filename, PyObject *name, int firstlineno,
49 PyObject *lnotab)
50{
51 PyCodeObject *co;
Martin v. Löwis18e16552006-02-15 17:27:45 +000052 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053 /* Check argument types */
54 if (argcount < 0 || nlocals < 0 ||
55 code == NULL ||
56 consts == NULL || !PyTuple_Check(consts) ||
57 names == NULL || !PyTuple_Check(names) ||
58 varnames == NULL || !PyTuple_Check(varnames) ||
59 freevars == NULL || !PyTuple_Check(freevars) ||
60 cellvars == NULL || !PyTuple_Check(cellvars) ||
61 name == NULL || !PyString_Check(name) ||
62 filename == NULL || !PyString_Check(filename) ||
63 lnotab == NULL || !PyString_Check(lnotab) ||
64 !PyObject_CheckReadBuffer(code)) {
65 PyErr_BadInternalCall();
66 return NULL;
67 }
68 intern_strings(names);
69 intern_strings(varnames);
70 intern_strings(freevars);
71 intern_strings(cellvars);
72 /* Intern selected string constants */
73 for (i = PyTuple_Size(consts); --i >= 0; ) {
74 PyObject *v = PyTuple_GetItem(consts, i);
75 if (!PyString_Check(v))
76 continue;
77 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
78 continue;
79 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
80 }
81 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
82 if (co != NULL) {
83 co->co_argcount = argcount;
Guido van Rossum4f72a782006-10-27 23:31:49 +000084 co->co_kwonlyargcount = kwonlyargcount;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085 co->co_nlocals = nlocals;
86 co->co_stacksize = stacksize;
87 co->co_flags = flags;
88 Py_INCREF(code);
89 co->co_code = code;
90 Py_INCREF(consts);
91 co->co_consts = consts;
92 Py_INCREF(names);
93 co->co_names = names;
94 Py_INCREF(varnames);
95 co->co_varnames = varnames;
96 Py_INCREF(freevars);
97 co->co_freevars = freevars;
98 Py_INCREF(cellvars);
99 co->co_cellvars = cellvars;
100 Py_INCREF(filename);
101 co->co_filename = filename;
102 Py_INCREF(name);
103 co->co_name = name;
104 co->co_firstlineno = firstlineno;
105 Py_INCREF(lnotab);
106 co->co_lnotab = lnotab;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000107 co->co_zombieframe = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108 }
109 return co;
110}
111
112
113#define OFF(x) offsetof(PyCodeObject, x)
114
115static PyMemberDef code_memberlist[] = {
116 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
Guido van Rossum4f72a782006-10-27 23:31:49 +0000117 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
119 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
120 {"co_flags", T_INT, OFF(co_flags), READONLY},
121 {"co_code", T_OBJECT, OFF(co_code), READONLY},
122 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
123 {"co_names", T_OBJECT, OFF(co_names), READONLY},
124 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
125 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
126 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
127 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
128 {"co_name", T_OBJECT, OFF(co_name), READONLY},
129 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
130 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
131 {NULL} /* Sentinel */
132};
133
134/* Helper for code_new: return a shallow copy of a tuple that is
135 guaranteed to contain exact strings, by converting string subclasses
136 to exact strings and complaining if a non-string is found. */
137static PyObject*
138validate_and_copy_tuple(PyObject *tup)
139{
140 PyObject *newtuple;
141 PyObject *item;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000142 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
144 len = PyTuple_GET_SIZE(tup);
145 newtuple = PyTuple_New(len);
146 if (newtuple == NULL)
147 return NULL;
148
149 for (i = 0; i < len; i++) {
150 item = PyTuple_GET_ITEM(tup, i);
151 if (PyString_CheckExact(item)) {
152 Py_INCREF(item);
153 }
154 else if (!PyString_Check(item)) {
155 PyErr_Format(
156 PyExc_TypeError,
157 "name tuples must contain only "
158 "strings, not '%.500s'",
159 item->ob_type->tp_name);
160 Py_DECREF(newtuple);
161 return NULL;
162 }
163 else {
164 item = PyString_FromStringAndSize(
165 PyString_AS_STRING(item),
166 PyString_GET_SIZE(item));
167 if (item == NULL) {
168 Py_DECREF(newtuple);
169 return NULL;
170 }
171 }
172 PyTuple_SET_ITEM(newtuple, i, item);
173 }
174
175 return newtuple;
176}
177
178PyDoc_STRVAR(code_doc,
179"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
180 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
181\n\
182Create a code object. Not for the faint of heart.");
183
184static PyObject *
185code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
186{
187 int argcount;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000188 int kwonlyargcount;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189 int nlocals;
190 int stacksize;
191 int flags;
192 PyObject *co = NULL;
193 PyObject *code;
194 PyObject *consts;
195 PyObject *names, *ournames = NULL;
196 PyObject *varnames, *ourvarnames = NULL;
197 PyObject *freevars = NULL, *ourfreevars = NULL;
198 PyObject *cellvars = NULL, *ourcellvars = NULL;
199 PyObject *filename;
200 PyObject *name;
201 int firstlineno;
202 PyObject *lnotab;
203
Guido van Rossum4f72a782006-10-27 23:31:49 +0000204 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!SSiS|O!O!:code",
205 &argcount, &kwonlyargcount,
206 &nlocals, &stacksize, &flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 &code,
208 &PyTuple_Type, &consts,
209 &PyTuple_Type, &names,
210 &PyTuple_Type, &varnames,
211 &filename, &name,
212 &firstlineno, &lnotab,
213 &PyTuple_Type, &freevars,
214 &PyTuple_Type, &cellvars))
215 return NULL;
216
217 if (argcount < 0) {
218 PyErr_SetString(
219 PyExc_ValueError,
220 "code: argcount must not be negative");
221 goto cleanup;
222 }
223
Guido van Rossum4f72a782006-10-27 23:31:49 +0000224 if (kwonlyargcount < 0) {
225 PyErr_SetString(
226 PyExc_ValueError,
227 "code: kwonlyargcount must not be negative");
228 goto cleanup;
229 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 if (nlocals < 0) {
231 PyErr_SetString(
232 PyExc_ValueError,
233 "code: nlocals must not be negative");
234 goto cleanup;
235 }
236
237 ournames = validate_and_copy_tuple(names);
238 if (ournames == NULL)
239 goto cleanup;
240 ourvarnames = validate_and_copy_tuple(varnames);
241 if (ourvarnames == NULL)
242 goto cleanup;
243 if (freevars)
244 ourfreevars = validate_and_copy_tuple(freevars);
245 else
246 ourfreevars = PyTuple_New(0);
247 if (ourfreevars == NULL)
248 goto cleanup;
249 if (cellvars)
250 ourcellvars = validate_and_copy_tuple(cellvars);
251 else
252 ourcellvars = PyTuple_New(0);
253 if (ourcellvars == NULL)
254 goto cleanup;
255
Guido van Rossum4f72a782006-10-27 23:31:49 +0000256 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
257 nlocals, stacksize, flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 code, consts, ournames, ourvarnames,
259 ourfreevars, ourcellvars, filename,
260 name, firstlineno, lnotab);
261 cleanup:
262 Py_XDECREF(ournames);
263 Py_XDECREF(ourvarnames);
264 Py_XDECREF(ourfreevars);
265 Py_XDECREF(ourcellvars);
266 return co;
267}
268
269static void
270code_dealloc(PyCodeObject *co)
271{
272 Py_XDECREF(co->co_code);
273 Py_XDECREF(co->co_consts);
274 Py_XDECREF(co->co_names);
275 Py_XDECREF(co->co_varnames);
276 Py_XDECREF(co->co_freevars);
277 Py_XDECREF(co->co_cellvars);
278 Py_XDECREF(co->co_filename);
279 Py_XDECREF(co->co_name);
280 Py_XDECREF(co->co_lnotab);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000281 if (co->co_zombieframe != NULL)
282 PyObject_GC_Del(co->co_zombieframe);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 PyObject_DEL(co);
284}
285
286static PyObject *
287code_repr(PyCodeObject *co)
288{
289 char buf[500];
290 int lineno = -1;
291 char *filename = "???";
292 char *name = "???";
293
294 if (co->co_firstlineno != 0)
295 lineno = co->co_firstlineno;
296 if (co->co_filename && PyString_Check(co->co_filename))
297 filename = PyString_AS_STRING(co->co_filename);
298 if (co->co_name && PyString_Check(co->co_name))
299 name = PyString_AS_STRING(co->co_name);
300 PyOS_snprintf(buf, sizeof(buf),
301 "<code object %.100s at %p, file \"%.300s\", line %d>",
302 name, co, filename, lineno);
303 return PyString_FromString(buf);
304}
305
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000306static PyObject *
307code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308{
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000309 PyCodeObject *co, *cp;
310 int eq;
311 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000312
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000313 if ((op != Py_EQ && op != Py_NE) ||
314 !PyCode_Check(self) ||
315 !PyCode_Check(other)) {
316 Py_INCREF(Py_NotImplemented);
317 return Py_NotImplemented;
318 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000320 co = (PyCodeObject *)self;
321 cp = (PyCodeObject *)other;
322
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000323 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000324 if (eq <= 0) goto unequal;
325 eq = co->co_argcount == cp->co_argcount;
326 if (!eq) goto unequal;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000327 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
328 if (!eq) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000329 eq = co->co_nlocals == cp->co_nlocals;
330 if (!eq) goto unequal;
331 eq = co->co_flags == cp->co_flags;
332 if (!eq) goto unequal;
333 eq = co->co_firstlineno == cp->co_firstlineno;
334 if (!eq) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000335 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000336 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000337 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000338 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000339 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000340 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000341 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000342 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000343 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000344 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000345 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000346 if (eq <= 0) goto unequal;
347
348 if (op == Py_EQ)
349 res = Py_True;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350 else
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000351 res = Py_False;
352 goto done;
353
354 unequal:
355 if (eq < 0)
356 return NULL;
357 if (op == Py_NE)
358 res = Py_True;
359 else
360 res = Py_False;
361
362 done:
363 Py_INCREF(res);
364 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365}
366
367static long
368code_hash(PyCodeObject *co)
369{
370 long h, h0, h1, h2, h3, h4, h5, h6;
371 h0 = PyObject_Hash(co->co_name);
372 if (h0 == -1) return -1;
373 h1 = PyObject_Hash(co->co_code);
374 if (h1 == -1) return -1;
375 h2 = PyObject_Hash(co->co_consts);
376 if (h2 == -1) return -1;
377 h3 = PyObject_Hash(co->co_names);
378 if (h3 == -1) return -1;
379 h4 = PyObject_Hash(co->co_varnames);
380 if (h4 == -1) return -1;
381 h5 = PyObject_Hash(co->co_freevars);
382 if (h5 == -1) return -1;
383 h6 = PyObject_Hash(co->co_cellvars);
384 if (h6 == -1) return -1;
385 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum4f72a782006-10-27 23:31:49 +0000386 co->co_argcount ^ co->co_kwonlyargcount ^
387 co->co_nlocals ^ co->co_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388 if (h == -1) h = -2;
389 return h;
390}
391
392/* XXX code objects need to participate in GC? */
393
394PyTypeObject PyCode_Type = {
395 PyObject_HEAD_INIT(&PyType_Type)
396 0,
397 "code",
398 sizeof(PyCodeObject),
399 0,
400 (destructor)code_dealloc, /* tp_dealloc */
401 0, /* tp_print */
402 0, /* tp_getattr */
403 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000404 0, /* tp_compare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405 (reprfunc)code_repr, /* tp_repr */
406 0, /* tp_as_number */
407 0, /* tp_as_sequence */
408 0, /* tp_as_mapping */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000409 (hashfunc)code_hash, /* tp_hash */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410 0, /* tp_call */
411 0, /* tp_str */
412 PyObject_GenericGetAttr, /* tp_getattro */
413 0, /* tp_setattro */
414 0, /* tp_as_buffer */
415 Py_TPFLAGS_DEFAULT, /* tp_flags */
416 code_doc, /* tp_doc */
417 0, /* tp_traverse */
418 0, /* tp_clear */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000419 code_richcompare, /* tp_richcompare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 0, /* tp_weaklistoffset */
421 0, /* tp_iter */
422 0, /* tp_iternext */
423 0, /* tp_methods */
424 code_memberlist, /* tp_members */
425 0, /* tp_getset */
426 0, /* tp_base */
427 0, /* tp_dict */
428 0, /* tp_descr_get */
429 0, /* tp_descr_set */
430 0, /* tp_dictoffset */
431 0, /* tp_init */
432 0, /* tp_alloc */
433 code_new, /* tp_new */
434};
435
436/* All about c_lnotab.
437
438c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
439mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
440to source code line #s (when needed for tracebacks) via c_lnotab instead.
441The array is conceptually a list of
442 (bytecode offset increment, line number increment)
443pairs. The details are important and delicate, best illustrated by example:
444
445 byte code offset source code line number
446 0 1
447 6 2
448 50 7
449 350 307
450 361 308
451
452The first trick is that these numbers aren't stored, only the increments
453from one row to the next (this doesn't really work, but it's a start):
454
455 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
456
457The second trick is that an unsigned byte can't hold negative values, or
458values larger than 255, so (a) there's a deep assumption that byte code
459offsets and their corresponding line #s both increase monotonically, and (b)
460if at least one column jumps by more than 255 from one row to the next, more
461than one pair is written to the table. In case #b, there's no way to know
462from looking at the table later how many were written. That's the delicate
463part. A user of c_lnotab desiring to find the source line number
464corresponding to a bytecode address A should do something like this
465
466 lineno = addr = 0
467 for addr_incr, line_incr in c_lnotab:
468 addr += addr_incr
469 if addr > A:
470 return lineno
471 lineno += line_incr
472
473In order for this to work, when the addr field increments by more than 255,
474the line # increment in each pair generated must be 0 until the remaining addr
475increment is < 256. So, in the example above, com_set_lineno should not (as
476was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
477255, 0, 45, 255, 0, 45.
478*/
479
480int
481PyCode_Addr2Line(PyCodeObject *co, int addrq)
482{
483 int size = PyString_Size(co->co_lnotab) / 2;
484 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
485 int line = co->co_firstlineno;
486 int addr = 0;
487 while (--size >= 0) {
488 addr += *p++;
489 if (addr > addrq)
490 break;
491 line += *p++;
492 }
493 return line;
494}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000495
496/*
497 Check whether the current instruction is at the start of a line.
498
499 */
500
501 /* The theory of SET_LINENO-less tracing.
502
503 In a nutshell, we use the co_lnotab field of the code object
504 to tell when execution has moved onto a different line.
505
506 As mentioned above, the basic idea is so set things up so
507 that
508
509 *instr_lb <= frame->f_lasti < *instr_ub
510
511 is true so long as execution does not change lines.
512
513 This is all fairly simple. Digging the information out of
514 co_lnotab takes some work, but is conceptually clear.
515
516 Somewhat harder to explain is why we don't *always* call the
517 line trace function when the above test fails.
518
519 Consider this code:
520
521 1: def f(a):
522 2: if a:
523 3: print 1
524 4: else:
525 5: print 2
526
527 which compiles to this:
528
529 2 0 LOAD_FAST 0 (a)
530 3 JUMP_IF_FALSE 9 (to 15)
531 6 POP_TOP
532
533 3 7 LOAD_CONST 1 (1)
534 10 PRINT_ITEM
535 11 PRINT_NEWLINE
536 12 JUMP_FORWARD 6 (to 21)
537 >> 15 POP_TOP
538
539 5 16 LOAD_CONST 2 (2)
540 19 PRINT_ITEM
541 20 PRINT_NEWLINE
542 >> 21 LOAD_CONST 0 (None)
543 24 RETURN_VALUE
544
545 If 'a' is false, execution will jump to instruction at offset
546 15 and the co_lnotab will claim that execution has moved to
547 line 3. This is at best misleading. In this case we could
548 associate the POP_TOP with line 4, but that doesn't make
549 sense in all cases (I think).
550
551 What we do is only call the line trace function if the co_lnotab
552 indicates we have jumped to the *start* of a line, i.e. if the
553 current instruction offset matches the offset given for the
554 start of a line by the co_lnotab.
555
556 This also takes care of the situation where 'a' is true.
557 Execution will jump from instruction offset 12 to offset 21.
558 Then the co_lnotab would imply that execution has moved to line
559 5, which is again misleading.
560
561 Why do we set f_lineno when tracing? Well, consider the code
562 above when 'a' is true. If stepping through this with 'n' in
563 pdb, you would stop at line 1 with a "call" type event, then
564 line events on lines 2 and 3, then a "return" type event -- but
565 you would be shown line 5 during this event. This is a change
566 from the behaviour in 2.2 and before, and I've found it
567 confusing in practice. By setting and using f_lineno when
568 tracing, one can report a line number different from that
569 suggested by f_lasti on this one occasion where it's desirable.
570 */
571
572
573int
574PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
575{
576 int size, addr, line;
577 unsigned char* p;
578
579 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
580 size = PyString_GET_SIZE(co->co_lnotab) / 2;
581
582 addr = 0;
583 line = co->co_firstlineno;
584 assert(line > 0);
585
586 /* possible optimization: if f->f_lasti == instr_ub
587 (likely to be a common case) then we already know
588 instr_lb -- if we stored the matching value of p
589 somwhere we could skip the first while loop. */
590
591 /* see comments in compile.c for the description of
592 co_lnotab. A point to remember: increments to p
593 should come in pairs -- although we don't care about
594 the line increments here, treating them as byte
595 increments gets confusing, to say the least. */
596
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000597 bounds->ap_lower = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 while (size > 0) {
599 if (addr + *p > lasti)
600 break;
601 addr += *p++;
602 if (*p)
603 bounds->ap_lower = addr;
604 line += *p++;
605 --size;
606 }
607
608 /* If lasti and addr don't match exactly, we don't want to
609 change the lineno slot on the frame or execute a trace
610 function. Return -1 instead.
611 */
612 if (addr != lasti)
613 line = -1;
614
615 if (size > 0) {
616 while (--size >= 0) {
617 addr += *p++;
618 if (*p++)
619 break;
620 }
621 bounds->ap_upper = addr;
622 }
623 else {
624 bounds->ap_upper = INT_MAX;
625 }
626
627 return line;
628}