blob: 80c2df90cbdbdbd2927c091a3c443bd545dede8f [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
Guido van Rossum98297ee2007-11-06 21:34:58 +000011all_name_chars(Py_UNICODE *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012{
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) {
Guido van Rossum98297ee2007-11-06 21:34:58 +000022 if (*s >= 128)
23 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 if (ok_name_char[*s++] == 0)
25 return 0;
26 }
27 return 1;
28}
29
30static void
31intern_strings(PyObject *tuple)
32{
Martin v. Löwis18e16552006-02-15 17:27:45 +000033 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034
35 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
36 PyObject *v = PyTuple_GET_ITEM(tuple, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +000037 if (v == NULL || !PyUnicode_CheckExact(v)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000038 Py_FatalError("non-string found in code slot");
39 }
Martin v. Löwis5b222132007-06-10 09:51:05 +000040 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041 }
42}
43
44
45PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000046PyCode_New(int argcount, int kwonlyargcount,
47 int nlocals, int stacksize, int flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048 PyObject *code, PyObject *consts, PyObject *names,
49 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
50 PyObject *filename, PyObject *name, int firstlineno,
51 PyObject *lnotab)
52{
53 PyCodeObject *co;
Martin v. Löwis18e16552006-02-15 17:27:45 +000054 Py_ssize_t i;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 /* Check argument types */
57 if (argcount < 0 || nlocals < 0 ||
58 code == NULL ||
59 consts == NULL || !PyTuple_Check(consts) ||
60 names == NULL || !PyTuple_Check(names) ||
61 varnames == NULL || !PyTuple_Check(varnames) ||
62 freevars == NULL || !PyTuple_Check(freevars) ||
63 cellvars == NULL || !PyTuple_Check(cellvars) ||
Guido van Rossum00bc0e02007-10-15 02:52:41 +000064 name == NULL || !PyUnicode_Check(name) ||
65 filename == NULL || !PyUnicode_Check(filename) ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000066 lnotab == NULL || !PyString_Check(lnotab) ||
67 !PyObject_CheckReadBuffer(code)) {
68 PyErr_BadInternalCall();
69 return NULL;
70 }
71 intern_strings(names);
72 intern_strings(varnames);
73 intern_strings(freevars);
74 intern_strings(cellvars);
75 /* Intern selected string constants */
76 for (i = PyTuple_Size(consts); --i >= 0; ) {
77 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum98297ee2007-11-06 21:34:58 +000078 if (!PyUnicode_Check(v))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079 continue;
Guido van Rossum98297ee2007-11-06 21:34:58 +000080 if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081 continue;
Guido van Rossum98297ee2007-11-06 21:34:58 +000082 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083 }
84 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
85 if (co != NULL) {
86 co->co_argcount = argcount;
Guido van Rossum4f72a782006-10-27 23:31:49 +000087 co->co_kwonlyargcount = kwonlyargcount;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088 co->co_nlocals = nlocals;
89 co->co_stacksize = stacksize;
90 co->co_flags = flags;
91 Py_INCREF(code);
92 co->co_code = code;
93 Py_INCREF(consts);
94 co->co_consts = consts;
95 Py_INCREF(names);
96 co->co_names = names;
97 Py_INCREF(varnames);
98 co->co_varnames = varnames;
99 Py_INCREF(freevars);
100 co->co_freevars = freevars;
101 Py_INCREF(cellvars);
102 co->co_cellvars = cellvars;
103 Py_INCREF(filename);
104 co->co_filename = filename;
105 Py_INCREF(name);
106 co->co_name = name;
107 co->co_firstlineno = firstlineno;
108 Py_INCREF(lnotab);
109 co->co_lnotab = lnotab;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110 co->co_zombieframe = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111 }
112 return co;
113}
114
115
116#define OFF(x) offsetof(PyCodeObject, x)
117
118static PyMemberDef code_memberlist[] = {
119 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
Guido van Rossum4f72a782006-10-27 23:31:49 +0000120 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
122 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
123 {"co_flags", T_INT, OFF(co_flags), READONLY},
124 {"co_code", T_OBJECT, OFF(co_code), READONLY},
125 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
126 {"co_names", T_OBJECT, OFF(co_names), READONLY},
127 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
128 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
129 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
130 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
131 {"co_name", T_OBJECT, OFF(co_name), READONLY},
132 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
133 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
134 {NULL} /* Sentinel */
135};
136
137/* Helper for code_new: return a shallow copy of a tuple that is
138 guaranteed to contain exact strings, by converting string subclasses
139 to exact strings and complaining if a non-string is found. */
140static PyObject*
141validate_and_copy_tuple(PyObject *tup)
142{
143 PyObject *newtuple;
144 PyObject *item;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000145 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
147 len = PyTuple_GET_SIZE(tup);
148 newtuple = PyTuple_New(len);
149 if (newtuple == NULL)
150 return NULL;
151
152 for (i = 0; i < len; i++) {
153 item = PyTuple_GET_ITEM(tup, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000154 if (PyUnicode_CheckExact(item)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000155 Py_INCREF(item);
156 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000157 else if (!PyUnicode_Check(item)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000158 PyErr_Format(
159 PyExc_TypeError,
160 "name tuples must contain only "
161 "strings, not '%.500s'",
162 item->ob_type->tp_name);
163 Py_DECREF(newtuple);
164 return NULL;
165 }
166 else {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000167 item = PyUnicode_FromUnicode(
168 PyUnicode_AS_UNICODE(item),
169 PyUnicode_GET_SIZE(item));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170 if (item == NULL) {
171 Py_DECREF(newtuple);
172 return NULL;
173 }
174 }
175 PyTuple_SET_ITEM(newtuple, i, item);
176 }
177
178 return newtuple;
179}
180
181PyDoc_STRVAR(code_doc,
182"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
183 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
184\n\
185Create a code object. Not for the faint of heart.");
186
187static PyObject *
188code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
189{
190 int argcount;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000191 int kwonlyargcount;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192 int nlocals;
193 int stacksize;
194 int flags;
195 PyObject *co = NULL;
196 PyObject *code;
197 PyObject *consts;
198 PyObject *names, *ournames = NULL;
199 PyObject *varnames, *ourvarnames = NULL;
200 PyObject *freevars = NULL, *ourfreevars = NULL;
201 PyObject *cellvars = NULL, *ourcellvars = NULL;
202 PyObject *filename;
203 PyObject *name;
204 int firstlineno;
205 PyObject *lnotab;
206
Guido van Rossum98297ee2007-11-06 21:34:58 +0000207 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000208 &argcount, &kwonlyargcount,
209 &nlocals, &stacksize, &flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 &code,
211 &PyTuple_Type, &consts,
212 &PyTuple_Type, &names,
213 &PyTuple_Type, &varnames,
214 &filename, &name,
215 &firstlineno, &lnotab,
216 &PyTuple_Type, &freevars,
217 &PyTuple_Type, &cellvars))
218 return NULL;
219
220 if (argcount < 0) {
221 PyErr_SetString(
222 PyExc_ValueError,
223 "code: argcount must not be negative");
224 goto cleanup;
225 }
226
Guido van Rossum4f72a782006-10-27 23:31:49 +0000227 if (kwonlyargcount < 0) {
228 PyErr_SetString(
229 PyExc_ValueError,
230 "code: kwonlyargcount must not be negative");
231 goto cleanup;
232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 if (nlocals < 0) {
234 PyErr_SetString(
235 PyExc_ValueError,
236 "code: nlocals must not be negative");
237 goto cleanup;
238 }
239
240 ournames = validate_and_copy_tuple(names);
241 if (ournames == NULL)
242 goto cleanup;
243 ourvarnames = validate_and_copy_tuple(varnames);
244 if (ourvarnames == NULL)
245 goto cleanup;
246 if (freevars)
247 ourfreevars = validate_and_copy_tuple(freevars);
248 else
249 ourfreevars = PyTuple_New(0);
250 if (ourfreevars == NULL)
251 goto cleanup;
252 if (cellvars)
253 ourcellvars = validate_and_copy_tuple(cellvars);
254 else
255 ourcellvars = PyTuple_New(0);
256 if (ourcellvars == NULL)
257 goto cleanup;
258
Guido van Rossum4f72a782006-10-27 23:31:49 +0000259 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
260 nlocals, stacksize, flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 code, consts, ournames, ourvarnames,
262 ourfreevars, ourcellvars, filename,
263 name, firstlineno, lnotab);
264 cleanup:
265 Py_XDECREF(ournames);
266 Py_XDECREF(ourvarnames);
267 Py_XDECREF(ourfreevars);
268 Py_XDECREF(ourcellvars);
269 return co;
270}
271
272static void
273code_dealloc(PyCodeObject *co)
274{
275 Py_XDECREF(co->co_code);
276 Py_XDECREF(co->co_consts);
277 Py_XDECREF(co->co_names);
278 Py_XDECREF(co->co_varnames);
279 Py_XDECREF(co->co_freevars);
280 Py_XDECREF(co->co_cellvars);
281 Py_XDECREF(co->co_filename);
282 Py_XDECREF(co->co_name);
283 Py_XDECREF(co->co_lnotab);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000284 if (co->co_zombieframe != NULL)
285 PyObject_GC_Del(co->co_zombieframe);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286 PyObject_DEL(co);
287}
288
289static PyObject *
290code_repr(PyCodeObject *co)
291{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292 int lineno = -1;
293 char *filename = "???";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
295 if (co->co_firstlineno != 0)
296 lineno = co->co_firstlineno;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000297 if (co->co_filename && PyUnicode_Check(co->co_filename))
298 filename = PyUnicode_AsString(co->co_filename);
Walter Dörwald933daed2007-06-06 15:15:34 +0000299 return PyUnicode_FromFormat(
Neal Norwitz41103bf2007-08-24 23:12:06 +0000300 "<code object %.100U at %p, file \"%.300s\", line %d>",
301 co->co_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 = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000393 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394 "code",
395 sizeof(PyCodeObject),
396 0,
397 (destructor)code_dealloc, /* tp_dealloc */
398 0, /* tp_print */
399 0, /* tp_getattr */
400 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000401 0, /* tp_compare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402 (reprfunc)code_repr, /* tp_repr */
403 0, /* tp_as_number */
404 0, /* tp_as_sequence */
405 0, /* tp_as_mapping */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000406 (hashfunc)code_hash, /* tp_hash */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 0, /* tp_call */
408 0, /* tp_str */
409 PyObject_GenericGetAttr, /* tp_getattro */
410 0, /* tp_setattro */
411 0, /* tp_as_buffer */
412 Py_TPFLAGS_DEFAULT, /* tp_flags */
413 code_doc, /* tp_doc */
414 0, /* tp_traverse */
415 0, /* tp_clear */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000416 code_richcompare, /* tp_richcompare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 0, /* tp_weaklistoffset */
418 0, /* tp_iter */
419 0, /* tp_iternext */
420 0, /* tp_methods */
421 code_memberlist, /* tp_members */
422 0, /* tp_getset */
423 0, /* tp_base */
424 0, /* tp_dict */
425 0, /* tp_descr_get */
426 0, /* tp_descr_set */
427 0, /* tp_dictoffset */
428 0, /* tp_init */
429 0, /* tp_alloc */
430 code_new, /* tp_new */
431};
432
433/* All about c_lnotab.
434
435c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
436mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
437to source code line #s (when needed for tracebacks) via c_lnotab instead.
438The array is conceptually a list of
439 (bytecode offset increment, line number increment)
440pairs. The details are important and delicate, best illustrated by example:
441
442 byte code offset source code line number
443 0 1
444 6 2
445 50 7
446 350 307
447 361 308
448
449The first trick is that these numbers aren't stored, only the increments
450from one row to the next (this doesn't really work, but it's a start):
451
452 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
453
454The second trick is that an unsigned byte can't hold negative values, or
455values larger than 255, so (a) there's a deep assumption that byte code
456offsets and their corresponding line #s both increase monotonically, and (b)
457if at least one column jumps by more than 255 from one row to the next, more
458than one pair is written to the table. In case #b, there's no way to know
459from looking at the table later how many were written. That's the delicate
460part. A user of c_lnotab desiring to find the source line number
461corresponding to a bytecode address A should do something like this
462
463 lineno = addr = 0
464 for addr_incr, line_incr in c_lnotab:
465 addr += addr_incr
466 if addr > A:
467 return lineno
468 lineno += line_incr
469
470In order for this to work, when the addr field increments by more than 255,
471the line # increment in each pair generated must be 0 until the remaining addr
472increment is < 256. So, in the example above, com_set_lineno should not (as
473was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
474255, 0, 45, 255, 0, 45.
475*/
476
477int
478PyCode_Addr2Line(PyCodeObject *co, int addrq)
479{
480 int size = PyString_Size(co->co_lnotab) / 2;
481 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
482 int line = co->co_firstlineno;
483 int addr = 0;
484 while (--size >= 0) {
485 addr += *p++;
486 if (addr > addrq)
487 break;
488 line += *p++;
489 }
490 return line;
491}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492
493/*
494 Check whether the current instruction is at the start of a line.
495
496 */
497
498 /* The theory of SET_LINENO-less tracing.
499
500 In a nutshell, we use the co_lnotab field of the code object
501 to tell when execution has moved onto a different line.
502
503 As mentioned above, the basic idea is so set things up so
504 that
505
506 *instr_lb <= frame->f_lasti < *instr_ub
507
508 is true so long as execution does not change lines.
509
510 This is all fairly simple. Digging the information out of
511 co_lnotab takes some work, but is conceptually clear.
512
513 Somewhat harder to explain is why we don't *always* call the
514 line trace function when the above test fails.
515
516 Consider this code:
517
518 1: def f(a):
519 2: if a:
520 3: print 1
521 4: else:
522 5: print 2
523
524 which compiles to this:
525
526 2 0 LOAD_FAST 0 (a)
527 3 JUMP_IF_FALSE 9 (to 15)
528 6 POP_TOP
529
530 3 7 LOAD_CONST 1 (1)
531 10 PRINT_ITEM
532 11 PRINT_NEWLINE
533 12 JUMP_FORWARD 6 (to 21)
534 >> 15 POP_TOP
535
536 5 16 LOAD_CONST 2 (2)
537 19 PRINT_ITEM
538 20 PRINT_NEWLINE
539 >> 21 LOAD_CONST 0 (None)
540 24 RETURN_VALUE
541
542 If 'a' is false, execution will jump to instruction at offset
543 15 and the co_lnotab will claim that execution has moved to
544 line 3. This is at best misleading. In this case we could
545 associate the POP_TOP with line 4, but that doesn't make
546 sense in all cases (I think).
547
548 What we do is only call the line trace function if the co_lnotab
549 indicates we have jumped to the *start* of a line, i.e. if the
550 current instruction offset matches the offset given for the
551 start of a line by the co_lnotab.
552
553 This also takes care of the situation where 'a' is true.
554 Execution will jump from instruction offset 12 to offset 21.
555 Then the co_lnotab would imply that execution has moved to line
556 5, which is again misleading.
557
558 Why do we set f_lineno when tracing? Well, consider the code
559 above when 'a' is true. If stepping through this with 'n' in
560 pdb, you would stop at line 1 with a "call" type event, then
561 line events on lines 2 and 3, then a "return" type event -- but
562 you would be shown line 5 during this event. This is a change
563 from the behaviour in 2.2 and before, and I've found it
564 confusing in practice. By setting and using f_lineno when
565 tracing, one can report a line number different from that
566 suggested by f_lasti on this one occasion where it's desirable.
567 */
568
569
570int
571PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
572{
573 int size, addr, line;
574 unsigned char* p;
575
576 p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
577 size = PyString_GET_SIZE(co->co_lnotab) / 2;
578
579 addr = 0;
580 line = co->co_firstlineno;
581 assert(line > 0);
582
583 /* possible optimization: if f->f_lasti == instr_ub
584 (likely to be a common case) then we already know
585 instr_lb -- if we stored the matching value of p
586 somwhere we could skip the first while loop. */
587
588 /* see comments in compile.c for the description of
589 co_lnotab. A point to remember: increments to p
590 should come in pairs -- although we don't care about
591 the line increments here, treating them as byte
592 increments gets confusing, to say the least. */
593
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000594 bounds->ap_lower = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 while (size > 0) {
596 if (addr + *p > lasti)
597 break;
598 addr += *p++;
599 if (*p)
600 bounds->ap_lower = addr;
601 line += *p++;
602 --size;
603 }
604
605 /* If lasti and addr don't match exactly, we don't want to
606 change the lineno slot on the frame or execute a trace
607 function. Return -1 instead.
608 */
609 if (addr != lasti)
610 line = -1;
611
612 if (size > 0) {
613 while (--size >= 0) {
614 addr += *p++;
615 if (*p++)
616 break;
617 }
618 bounds->ap_upper = addr;
619 }
620 else {
621 bounds->ap_upper = INT_MAX;
622 }
623
624 return line;
625}