blob: c7351930da35278850df790a8befc45a587713a9 [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);
Martin v. Löwis5b222132007-06-10 09:51:05 +000035 if (v == NULL || !PyUnicode_CheckExact(v)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 Py_FatalError("non-string found in code slot");
37 }
Martin v. Löwis5b222132007-06-10 09:51:05 +000038 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000039 }
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) ||
Martin v. Löwis5b222132007-06-10 09:51:05 +000061 name == NULL || (!PyString_Check(name) && !PyUnicode_Check(name)) ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062 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);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000151 if (PyUnicode_CheckExact(item)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152 Py_INCREF(item);
153 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000154 else if (!PyUnicode_Check(item)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000155 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 {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000164 item = PyUnicode_FromUnicode(
165 PyUnicode_AS_UNICODE(item),
166 PyUnicode_GET_SIZE(item));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167 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{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 int lineno = -1;
290 char *filename = "???";
291 char *name = "???";
292
293 if (co->co_firstlineno != 0)
294 lineno = co->co_firstlineno;
295 if (co->co_filename && PyString_Check(co->co_filename))
296 filename = PyString_AS_STRING(co->co_filename);
297 if (co->co_name && PyString_Check(co->co_name))
298 name = PyString_AS_STRING(co->co_name);
Walter Dörwald933daed2007-06-06 15:15:34 +0000299 return PyUnicode_FromFormat(
300 "<code object %.100s at %p, file \"%.300s\", line %d>",
301 name, co, filename, lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302}
303
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000304static PyObject *
305code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306{
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000307 PyCodeObject *co, *cp;
308 int eq;
309 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000310
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000311 if ((op != Py_EQ && op != Py_NE) ||
312 !PyCode_Check(self) ||
313 !PyCode_Check(other)) {
314 Py_INCREF(Py_NotImplemented);
315 return Py_NotImplemented;
316 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000318 co = (PyCodeObject *)self;
319 cp = (PyCodeObject *)other;
320
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000321 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000322 if (eq <= 0) goto unequal;
323 eq = co->co_argcount == cp->co_argcount;
324 if (!eq) goto unequal;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000325 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
326 if (!eq) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000327 eq = co->co_nlocals == cp->co_nlocals;
328 if (!eq) goto unequal;
329 eq = co->co_flags == cp->co_flags;
330 if (!eq) goto unequal;
331 eq = co->co_firstlineno == cp->co_firstlineno;
332 if (!eq) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000333 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000334 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000335 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, 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_names, cp->co_names, 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_varnames, cp->co_varnames, 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_freevars, cp->co_freevars, 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_cellvars, cp->co_cellvars, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000344 if (eq <= 0) goto unequal;
345
346 if (op == Py_EQ)
347 res = Py_True;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 else
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000349 res = Py_False;
350 goto done;
351
352 unequal:
353 if (eq < 0)
354 return NULL;
355 if (op == Py_NE)
356 res = Py_True;
357 else
358 res = Py_False;
359
360 done:
361 Py_INCREF(res);
362 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
365static long
366code_hash(PyCodeObject *co)
367{
368 long h, h0, h1, h2, h3, h4, h5, h6;
369 h0 = PyObject_Hash(co->co_name);
370 if (h0 == -1) return -1;
371 h1 = PyObject_Hash(co->co_code);
372 if (h1 == -1) return -1;
373 h2 = PyObject_Hash(co->co_consts);
374 if (h2 == -1) return -1;
375 h3 = PyObject_Hash(co->co_names);
376 if (h3 == -1) return -1;
377 h4 = PyObject_Hash(co->co_varnames);
378 if (h4 == -1) return -1;
379 h5 = PyObject_Hash(co->co_freevars);
380 if (h5 == -1) return -1;
381 h6 = PyObject_Hash(co->co_cellvars);
382 if (h6 == -1) return -1;
383 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum4f72a782006-10-27 23:31:49 +0000384 co->co_argcount ^ co->co_kwonlyargcount ^
385 co->co_nlocals ^ co->co_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 if (h == -1) h = -2;
387 return h;
388}
389
390/* XXX code objects need to participate in GC? */
391
392PyTypeObject PyCode_Type = {
393 PyObject_HEAD_INIT(&PyType_Type)
394 0,
395 "code",
396 sizeof(PyCodeObject),
397 0,
398 (destructor)code_dealloc, /* tp_dealloc */
399 0, /* tp_print */
400 0, /* tp_getattr */
401 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000402 0, /* tp_compare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403 (reprfunc)code_repr, /* tp_repr */
404 0, /* tp_as_number */
405 0, /* tp_as_sequence */
406 0, /* tp_as_mapping */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000407 (hashfunc)code_hash, /* tp_hash */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408 0, /* tp_call */
409 0, /* tp_str */
410 PyObject_GenericGetAttr, /* tp_getattro */
411 0, /* tp_setattro */
412 0, /* tp_as_buffer */
413 Py_TPFLAGS_DEFAULT, /* tp_flags */
414 code_doc, /* tp_doc */
415 0, /* tp_traverse */
416 0, /* tp_clear */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000417 code_richcompare, /* tp_richcompare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418 0, /* tp_weaklistoffset */
419 0, /* tp_iter */
420 0, /* tp_iternext */
421 0, /* tp_methods */
422 code_memberlist, /* tp_members */
423 0, /* tp_getset */
424 0, /* tp_base */
425 0, /* tp_dict */
426 0, /* tp_descr_get */
427 0, /* tp_descr_set */
428 0, /* tp_dictoffset */
429 0, /* tp_init */
430 0, /* tp_alloc */
431 code_new, /* tp_new */
432};
433
434/* All about c_lnotab.
435
436c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
437mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
438to source code line #s (when needed for tracebacks) via c_lnotab instead.
439The array is conceptually a list of
440 (bytecode offset increment, line number increment)
441pairs. The details are important and delicate, best illustrated by example:
442
443 byte code offset source code line number
444 0 1
445 6 2
446 50 7
447 350 307
448 361 308
449
450The first trick is that these numbers aren't stored, only the increments
451from one row to the next (this doesn't really work, but it's a start):
452
453 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
454
455The second trick is that an unsigned byte can't hold negative values, or
456values larger than 255, so (a) there's a deep assumption that byte code
457offsets and their corresponding line #s both increase monotonically, and (b)
458if at least one column jumps by more than 255 from one row to the next, more
459than one pair is written to the table. In case #b, there's no way to know
460from looking at the table later how many were written. That's the delicate
461part. A user of c_lnotab desiring to find the source line number
462corresponding to a bytecode address A should do something like this
463
464 lineno = addr = 0
465 for addr_incr, line_incr in c_lnotab:
466 addr += addr_incr
467 if addr > A:
468 return lineno
469 lineno += line_incr
470
471In order for this to work, when the addr field increments by more than 255,
472the line # increment in each pair generated must be 0 until the remaining addr
473increment is < 256. So, in the example above, com_set_lineno should not (as
474was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
475255, 0, 45, 255, 0, 45.
476*/
477
478int
479PyCode_Addr2Line(PyCodeObject *co, int addrq)
480{
481 int size = PyString_Size(co->co_lnotab) / 2;
482 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
483 int line = co->co_firstlineno;
484 int addr = 0;
485 while (--size >= 0) {
486 addr += *p++;
487 if (addr > addrq)
488 break;
489 line += *p++;
490 }
491 return line;
492}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000493
494/*
495 Check whether the current instruction is at the start of a line.
496
497 */
498
499 /* The theory of SET_LINENO-less tracing.
500
501 In a nutshell, we use the co_lnotab field of the code object
502 to tell when execution has moved onto a different line.
503
504 As mentioned above, the basic idea is so set things up so
505 that
506
507 *instr_lb <= frame->f_lasti < *instr_ub
508
509 is true so long as execution does not change lines.
510
511 This is all fairly simple. Digging the information out of
512 co_lnotab takes some work, but is conceptually clear.
513
514 Somewhat harder to explain is why we don't *always* call the
515 line trace function when the above test fails.
516
517 Consider this code:
518
519 1: def f(a):
520 2: if a:
521 3: print 1
522 4: else:
523 5: print 2
524
525 which compiles to this:
526
527 2 0 LOAD_FAST 0 (a)
528 3 JUMP_IF_FALSE 9 (to 15)
529 6 POP_TOP
530
531 3 7 LOAD_CONST 1 (1)
532 10 PRINT_ITEM
533 11 PRINT_NEWLINE
534 12 JUMP_FORWARD 6 (to 21)
535 >> 15 POP_TOP
536
537 5 16 LOAD_CONST 2 (2)
538 19 PRINT_ITEM
539 20 PRINT_NEWLINE
540 >> 21 LOAD_CONST 0 (None)
541 24 RETURN_VALUE
542
543 If 'a' is false, execution will jump to instruction at offset
544 15 and the co_lnotab will claim that execution has moved to
545 line 3. This is at best misleading. In this case we could
546 associate the POP_TOP with line 4, but that doesn't make
547 sense in all cases (I think).
548
549 What we do is only call the line trace function if the co_lnotab
550 indicates we have jumped to the *start* of a line, i.e. if the
551 current instruction offset matches the offset given for the
552 start of a line by the co_lnotab.
553
554 This also takes care of the situation where 'a' is true.
555 Execution will jump from instruction offset 12 to offset 21.
556 Then the co_lnotab would imply that execution has moved to line
557 5, which is again misleading.
558
559 Why do we set f_lineno when tracing? Well, consider the code
560 above when 'a' is true. If stepping through this with 'n' in
561 pdb, you would stop at line 1 with a "call" type event, then
562 line events on lines 2 and 3, then a "return" type event -- but
563 you would be shown line 5 during this event. This is a change
564 from the behaviour in 2.2 and before, and I've found it
565 confusing in practice. By setting and using f_lineno when
566 tracing, one can report a line number different from that
567 suggested by f_lasti on this one occasion where it's desirable.
568 */
569
570
571int
572PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
573{
574 int size, addr, line;
575 unsigned char* p;
576
577 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
578 size = PyString_GET_SIZE(co->co_lnotab) / 2;
579
580 addr = 0;
581 line = co->co_firstlineno;
582 assert(line > 0);
583
584 /* possible optimization: if f->f_lasti == instr_ub
585 (likely to be a common case) then we already know
586 instr_lb -- if we stored the matching value of p
587 somwhere we could skip the first while loop. */
588
589 /* see comments in compile.c for the description of
590 co_lnotab. A point to remember: increments to p
591 should come in pairs -- although we don't care about
592 the line increments here, treating them as byte
593 increments gets confusing, to say the least. */
594
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595 bounds->ap_lower = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000596 while (size > 0) {
597 if (addr + *p > lasti)
598 break;
599 addr += *p++;
600 if (*p)
601 bounds->ap_lower = addr;
602 line += *p++;
603 --size;
604 }
605
606 /* If lasti and addr don't match exactly, we don't want to
607 change the lineno slot on the frame or execute a trace
608 function. Return -1 instead.
609 */
610 if (addr != lasti)
611 line = -1;
612
613 if (size > 0) {
614 while (--size >= 0) {
615 addr += *p++;
616 if (*p++)
617 break;
618 }
619 bounds->ap_upper = addr;
620 }
621 else {
622 bounds->ap_upper = INT_MAX;
623 }
624
625 return line;
626}