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