blob: 54c23aec36d9ee3b9515ca2745b1a7421d71f51f [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;
54 Py_ssize_t i;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 /* Check argument types */
57 if (argcount < 0 || kwonlyargcount < 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) ||
64 name == NULL || !PyUnicode_Check(name) ||
65 filename == NULL || !PyUnicode_Check(filename) ||
66 lnotab == NULL || !PyBytes_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);
78 if (!PyUnicode_Check(v))
79 continue;
80 if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
81 continue;
82 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
83 }
84 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
85 if (co != NULL) {
86 co->co_argcount = argcount;
87 co->co_kwonlyargcount = kwonlyargcount;
88 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;
110 co->co_zombieframe = NULL;
111 co->co_weakreflist = NULL;
112 }
113 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114}
115
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000116PyCodeObject *
117PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 static PyObject *emptystring = NULL;
120 static PyObject *nulltuple = NULL;
121 PyObject *filename_ob = NULL;
122 PyObject *funcname_ob = NULL;
123 PyCodeObject *result = NULL;
124 if (emptystring == NULL) {
125 emptystring = PyBytes_FromString("");
126 if (emptystring == NULL)
127 goto failed;
128 }
129 if (nulltuple == NULL) {
130 nulltuple = PyTuple_New(0);
131 if (nulltuple == NULL)
132 goto failed;
133 }
134 funcname_ob = PyUnicode_FromString(funcname);
135 if (funcname_ob == NULL)
136 goto failed;
137 filename_ob = PyUnicode_DecodeFSDefault(filename);
138 if (filename_ob == NULL)
139 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 result = PyCode_New(0, /* argcount */
142 0, /* kwonlyargcount */
143 0, /* nlocals */
144 0, /* stacksize */
145 0, /* flags */
146 emptystring, /* code */
147 nulltuple, /* consts */
148 nulltuple, /* names */
149 nulltuple, /* varnames */
150 nulltuple, /* freevars */
151 nulltuple, /* cellvars */
152 filename_ob, /* filename */
153 funcname_ob, /* name */
154 firstlineno, /* firstlineno */
155 emptystring /* lnotab */
156 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000157
158failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 Py_XDECREF(funcname_ob);
160 Py_XDECREF(filename_ob);
161 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000162}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164#define OFF(x) offsetof(PyCodeObject, x)
165
166static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
168 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
169 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
170 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
171 {"co_flags", T_INT, OFF(co_flags), READONLY},
172 {"co_code", T_OBJECT, OFF(co_code), READONLY},
173 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
174 {"co_names", T_OBJECT, OFF(co_names), READONLY},
175 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
176 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
177 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
178 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
179 {"co_name", T_OBJECT, OFF(co_name), READONLY},
180 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
181 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
182 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183};
184
185/* Helper for code_new: return a shallow copy of a tuple that is
186 guaranteed to contain exact strings, by converting string subclasses
187 to exact strings and complaining if a non-string is found. */
188static PyObject*
189validate_and_copy_tuple(PyObject *tup)
190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 PyObject *newtuple;
192 PyObject *item;
193 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 len = PyTuple_GET_SIZE(tup);
196 newtuple = PyTuple_New(len);
197 if (newtuple == NULL)
198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 for (i = 0; i < len; i++) {
201 item = PyTuple_GET_ITEM(tup, i);
202 if (PyUnicode_CheckExact(item)) {
203 Py_INCREF(item);
204 }
205 else if (!PyUnicode_Check(item)) {
206 PyErr_Format(
207 PyExc_TypeError,
208 "name tuples must contain only "
209 "strings, not '%.500s'",
210 item->ob_type->tp_name);
211 Py_DECREF(newtuple);
212 return NULL;
213 }
214 else {
215 item = PyUnicode_FromUnicode(
216 PyUnicode_AS_UNICODE(item),
217 PyUnicode_GET_SIZE(item));
218 if (item == NULL) {
219 Py_DECREF(newtuple);
220 return NULL;
221 }
222 }
223 PyTuple_SET_ITEM(newtuple, i, item);
224 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227}
228
229PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000230"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000231 constants, names, varnames, filename, name, firstlineno,\n\
232 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233\n\
234Create a code object. Not for the faint of heart.");
235
236static PyObject *
237code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 int argcount;
240 int kwonlyargcount;
241 int nlocals;
242 int stacksize;
243 int flags;
244 PyObject *co = NULL;
245 PyObject *code;
246 PyObject *consts;
247 PyObject *names, *ournames = NULL;
248 PyObject *varnames, *ourvarnames = NULL;
249 PyObject *freevars = NULL, *ourfreevars = NULL;
250 PyObject *cellvars = NULL, *ourcellvars = NULL;
251 PyObject *filename;
252 PyObject *name;
253 int firstlineno;
254 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
257 &argcount, &kwonlyargcount,
258 &nlocals, &stacksize, &flags,
259 &code,
260 &PyTuple_Type, &consts,
261 &PyTuple_Type, &names,
262 &PyTuple_Type, &varnames,
263 &filename, &name,
264 &firstlineno, &lnotab,
265 &PyTuple_Type, &freevars,
266 &PyTuple_Type, &cellvars))
267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (argcount < 0) {
270 PyErr_SetString(
271 PyExc_ValueError,
272 "code: argcount must not be negative");
273 goto cleanup;
274 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if (kwonlyargcount < 0) {
277 PyErr_SetString(
278 PyExc_ValueError,
279 "code: kwonlyargcount must not be negative");
280 goto cleanup;
281 }
282 if (nlocals < 0) {
283 PyErr_SetString(
284 PyExc_ValueError,
285 "code: nlocals must not be negative");
286 goto cleanup;
287 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 ournames = validate_and_copy_tuple(names);
290 if (ournames == NULL)
291 goto cleanup;
292 ourvarnames = validate_and_copy_tuple(varnames);
293 if (ourvarnames == NULL)
294 goto cleanup;
295 if (freevars)
296 ourfreevars = validate_and_copy_tuple(freevars);
297 else
298 ourfreevars = PyTuple_New(0);
299 if (ourfreevars == NULL)
300 goto cleanup;
301 if (cellvars)
302 ourcellvars = validate_and_copy_tuple(cellvars);
303 else
304 ourcellvars = PyTuple_New(0);
305 if (ourcellvars == NULL)
306 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
309 nlocals, stacksize, flags,
310 code, consts, ournames, ourvarnames,
311 ourfreevars, ourcellvars, filename,
312 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 Py_XDECREF(ournames);
315 Py_XDECREF(ourvarnames);
316 Py_XDECREF(ourfreevars);
317 Py_XDECREF(ourcellvars);
318 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
321static void
322code_dealloc(PyCodeObject *co)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 Py_XDECREF(co->co_code);
325 Py_XDECREF(co->co_consts);
326 Py_XDECREF(co->co_names);
327 Py_XDECREF(co->co_varnames);
328 Py_XDECREF(co->co_freevars);
329 Py_XDECREF(co->co_cellvars);
330 Py_XDECREF(co->co_filename);
331 Py_XDECREF(co->co_name);
332 Py_XDECREF(co->co_lnotab);
333 if (co->co_zombieframe != NULL)
334 PyObject_GC_Del(co->co_zombieframe);
335 if (co->co_weakreflist != NULL)
336 PyObject_ClearWeakRefs((PyObject*)co);
337 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338}
339
340static PyObject *
341code_repr(PyCodeObject *co)
342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 int lineno;
344 if (co->co_firstlineno != 0)
345 lineno = co->co_firstlineno;
346 else
347 lineno = -1;
348 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
349 return PyUnicode_FromFormat(
350 "<code object %.100U at %p, file \"%.300U\", line %d>",
351 co->co_name, co, co->co_filename, lineno);
352 } else {
353 return PyUnicode_FromFormat(
354 "<code object %.100U at %p, file ???, line %d>",
355 co->co_name, co, lineno);
356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357}
358
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000359static PyObject *
360code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 PyCodeObject *co, *cp;
363 int eq;
364 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if ((op != Py_EQ && op != Py_NE) ||
367 !PyCode_Check(self) ||
368 !PyCode_Check(other)) {
369 Py_INCREF(Py_NotImplemented);
370 return Py_NotImplemented;
371 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 co = (PyCodeObject *)self;
374 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
377 if (eq <= 0) goto unequal;
378 eq = co->co_argcount == cp->co_argcount;
379 if (!eq) goto unequal;
380 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
381 if (!eq) goto unequal;
382 eq = co->co_nlocals == cp->co_nlocals;
383 if (!eq) goto unequal;
384 eq = co->co_flags == cp->co_flags;
385 if (!eq) goto unequal;
386 eq = co->co_firstlineno == cp->co_firstlineno;
387 if (!eq) goto unequal;
388 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
389 if (eq <= 0) goto unequal;
390 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
391 if (eq <= 0) goto unequal;
392 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
393 if (eq <= 0) goto unequal;
394 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
395 if (eq <= 0) goto unequal;
396 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
397 if (eq <= 0) goto unequal;
398 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
399 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (op == Py_EQ)
402 res = Py_True;
403 else
404 res = Py_False;
405 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000406
407 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 if (eq < 0)
409 return NULL;
410 if (op == Py_NE)
411 res = Py_True;
412 else
413 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000414
415 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 Py_INCREF(res);
417 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418}
419
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000420static Py_hash_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421code_hash(PyCodeObject *co)
422{
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000423 Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 h0 = PyObject_Hash(co->co_name);
425 if (h0 == -1) return -1;
426 h1 = PyObject_Hash(co->co_code);
427 if (h1 == -1) return -1;
428 h2 = PyObject_Hash(co->co_consts);
429 if (h2 == -1) return -1;
430 h3 = PyObject_Hash(co->co_names);
431 if (h3 == -1) return -1;
432 h4 = PyObject_Hash(co->co_varnames);
433 if (h4 == -1) return -1;
434 h5 = PyObject_Hash(co->co_freevars);
435 if (h5 == -1) return -1;
436 h6 = PyObject_Hash(co->co_cellvars);
437 if (h6 == -1) return -1;
438 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
439 co->co_argcount ^ co->co_kwonlyargcount ^
440 co->co_nlocals ^ co->co_flags;
441 if (h == -1) h = -2;
442 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443}
444
445/* XXX code objects need to participate in GC? */
446
447PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyVarObject_HEAD_INIT(&PyType_Type, 0)
449 "code",
450 sizeof(PyCodeObject),
451 0,
452 (destructor)code_dealloc, /* tp_dealloc */
453 0, /* tp_print */
454 0, /* tp_getattr */
455 0, /* tp_setattr */
456 0, /* tp_reserved */
457 (reprfunc)code_repr, /* tp_repr */
458 0, /* tp_as_number */
459 0, /* tp_as_sequence */
460 0, /* tp_as_mapping */
461 (hashfunc)code_hash, /* tp_hash */
462 0, /* tp_call */
463 0, /* tp_str */
464 PyObject_GenericGetAttr, /* tp_getattro */
465 0, /* tp_setattro */
466 0, /* tp_as_buffer */
467 Py_TPFLAGS_DEFAULT, /* tp_flags */
468 code_doc, /* tp_doc */
469 0, /* tp_traverse */
470 0, /* tp_clear */
471 code_richcompare, /* tp_richcompare */
472 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
473 0, /* tp_iter */
474 0, /* tp_iternext */
475 0, /* tp_methods */
476 code_memberlist, /* tp_members */
477 0, /* tp_getset */
478 0, /* tp_base */
479 0, /* tp_dict */
480 0, /* tp_descr_get */
481 0, /* tp_descr_set */
482 0, /* tp_dictoffset */
483 0, /* tp_init */
484 0, /* tp_alloc */
485 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486};
487
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000488/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
489 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490*/
491
492int
493PyCode_Addr2Line(PyCodeObject *co, int addrq)
494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int size = PyBytes_Size(co->co_lnotab) / 2;
496 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
497 int line = co->co_firstlineno;
498 int addr = 0;
499 while (--size >= 0) {
500 addr += *p++;
501 if (addr > addrq)
502 break;
503 line += *p++;
504 }
505 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000508/* Update *bounds to describe the first and one-past-the-last instructions in
509 the same line as lasti. Return the number of that line. */
510int
511_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 int size, addr, line;
514 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
517 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 addr = 0;
520 line = co->co_firstlineno;
521 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 /* possible optimization: if f->f_lasti == instr_ub
524 (likely to be a common case) then we already know
525 instr_lb -- if we stored the matching value of p
526 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 /* See lnotab_notes.txt for the description of
529 co_lnotab. A point to remember: increments to p
530 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 bounds->ap_lower = 0;
533 while (size > 0) {
534 if (addr + *p > lasti)
535 break;
536 addr += *p++;
537 if (*p)
538 bounds->ap_lower = addr;
539 line += *p++;
540 --size;
541 }
542
543 if (size > 0) {
544 while (--size >= 0) {
545 addr += *p++;
546 if (*p++)
547 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 bounds->ap_upper = addr;
550 }
551 else {
552 bounds->ap_upper = INT_MAX;
553 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556}