blob: e24fc8d42b1c2fa5a84a571eed67e62a59b66860 [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;
Victor Stinnerc39211f2010-09-29 16:35:47 +0000114
115error:
116 Py_DECREF(co);
117 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118}
119
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000120PyCodeObject *
121PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 static PyObject *emptystring = NULL;
124 static PyObject *nulltuple = NULL;
125 PyObject *filename_ob = NULL;
126 PyObject *funcname_ob = NULL;
127 PyCodeObject *result = NULL;
128 if (emptystring == NULL) {
129 emptystring = PyBytes_FromString("");
130 if (emptystring == NULL)
131 goto failed;
132 }
133 if (nulltuple == NULL) {
134 nulltuple = PyTuple_New(0);
135 if (nulltuple == NULL)
136 goto failed;
137 }
138 funcname_ob = PyUnicode_FromString(funcname);
139 if (funcname_ob == NULL)
140 goto failed;
141 filename_ob = PyUnicode_DecodeFSDefault(filename);
142 if (filename_ob == NULL)
143 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 result = PyCode_New(0, /* argcount */
146 0, /* kwonlyargcount */
147 0, /* nlocals */
148 0, /* stacksize */
149 0, /* flags */
150 emptystring, /* code */
151 nulltuple, /* consts */
152 nulltuple, /* names */
153 nulltuple, /* varnames */
154 nulltuple, /* freevars */
155 nulltuple, /* cellvars */
156 filename_ob, /* filename */
157 funcname_ob, /* name */
158 firstlineno, /* firstlineno */
159 emptystring /* lnotab */
160 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000161
162failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 Py_XDECREF(funcname_ob);
164 Py_XDECREF(filename_ob);
165 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000166}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167
168#define OFF(x) offsetof(PyCodeObject, x)
169
170static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
172 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
173 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
174 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
175 {"co_flags", T_INT, OFF(co_flags), READONLY},
176 {"co_code", T_OBJECT, OFF(co_code), READONLY},
177 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
178 {"co_names", T_OBJECT, OFF(co_names), READONLY},
179 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
180 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
181 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
182 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
183 {"co_name", T_OBJECT, OFF(co_name), READONLY},
184 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
185 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
186 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187};
188
189/* Helper for code_new: return a shallow copy of a tuple that is
190 guaranteed to contain exact strings, by converting string subclasses
191 to exact strings and complaining if a non-string is found. */
192static PyObject*
193validate_and_copy_tuple(PyObject *tup)
194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyObject *newtuple;
196 PyObject *item;
197 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 len = PyTuple_GET_SIZE(tup);
200 newtuple = PyTuple_New(len);
201 if (newtuple == NULL)
202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 for (i = 0; i < len; i++) {
205 item = PyTuple_GET_ITEM(tup, i);
206 if (PyUnicode_CheckExact(item)) {
207 Py_INCREF(item);
208 }
209 else if (!PyUnicode_Check(item)) {
210 PyErr_Format(
211 PyExc_TypeError,
212 "name tuples must contain only "
213 "strings, not '%.500s'",
214 item->ob_type->tp_name);
215 Py_DECREF(newtuple);
216 return NULL;
217 }
218 else {
219 item = PyUnicode_FromUnicode(
220 PyUnicode_AS_UNICODE(item),
221 PyUnicode_GET_SIZE(item));
222 if (item == NULL) {
223 Py_DECREF(newtuple);
224 return NULL;
225 }
226 }
227 PyTuple_SET_ITEM(newtuple, i, item);
228 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231}
232
233PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000234"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000235 constants, names, varnames, filename, name, firstlineno,\n\
236 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237\n\
238Create a code object. Not for the faint of heart.");
239
240static PyObject *
241code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 int argcount;
244 int kwonlyargcount;
245 int nlocals;
246 int stacksize;
247 int flags;
248 PyObject *co = NULL;
249 PyObject *code;
250 PyObject *consts;
251 PyObject *names, *ournames = NULL;
252 PyObject *varnames, *ourvarnames = NULL;
253 PyObject *freevars = NULL, *ourfreevars = NULL;
254 PyObject *cellvars = NULL, *ourcellvars = NULL;
255 PyObject *filename;
256 PyObject *name;
257 int firstlineno;
258 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
261 &argcount, &kwonlyargcount,
262 &nlocals, &stacksize, &flags,
263 &code,
264 &PyTuple_Type, &consts,
265 &PyTuple_Type, &names,
266 &PyTuple_Type, &varnames,
267 &filename, &name,
268 &firstlineno, &lnotab,
269 &PyTuple_Type, &freevars,
270 &PyTuple_Type, &cellvars))
271 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (argcount < 0) {
274 PyErr_SetString(
275 PyExc_ValueError,
276 "code: argcount must not be negative");
277 goto cleanup;
278 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (kwonlyargcount < 0) {
281 PyErr_SetString(
282 PyExc_ValueError,
283 "code: kwonlyargcount must not be negative");
284 goto cleanup;
285 }
286 if (nlocals < 0) {
287 PyErr_SetString(
288 PyExc_ValueError,
289 "code: nlocals must not be negative");
290 goto cleanup;
291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 ournames = validate_and_copy_tuple(names);
294 if (ournames == NULL)
295 goto cleanup;
296 ourvarnames = validate_and_copy_tuple(varnames);
297 if (ourvarnames == NULL)
298 goto cleanup;
299 if (freevars)
300 ourfreevars = validate_and_copy_tuple(freevars);
301 else
302 ourfreevars = PyTuple_New(0);
303 if (ourfreevars == NULL)
304 goto cleanup;
305 if (cellvars)
306 ourcellvars = validate_and_copy_tuple(cellvars);
307 else
308 ourcellvars = PyTuple_New(0);
309 if (ourcellvars == NULL)
310 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
313 nlocals, stacksize, flags,
314 code, consts, ournames, ourvarnames,
315 ourfreevars, ourcellvars, filename,
316 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 Py_XDECREF(ournames);
319 Py_XDECREF(ourvarnames);
320 Py_XDECREF(ourfreevars);
321 Py_XDECREF(ourcellvars);
322 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323}
324
325static void
326code_dealloc(PyCodeObject *co)
327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_XDECREF(co->co_code);
329 Py_XDECREF(co->co_consts);
330 Py_XDECREF(co->co_names);
331 Py_XDECREF(co->co_varnames);
332 Py_XDECREF(co->co_freevars);
333 Py_XDECREF(co->co_cellvars);
334 Py_XDECREF(co->co_filename);
335 Py_XDECREF(co->co_name);
336 Py_XDECREF(co->co_lnotab);
337 if (co->co_zombieframe != NULL)
338 PyObject_GC_Del(co->co_zombieframe);
339 if (co->co_weakreflist != NULL)
340 PyObject_ClearWeakRefs((PyObject*)co);
341 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342}
343
344static PyObject *
345code_repr(PyCodeObject *co)
346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 int lineno;
348 if (co->co_firstlineno != 0)
349 lineno = co->co_firstlineno;
350 else
351 lineno = -1;
352 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
353 return PyUnicode_FromFormat(
354 "<code object %.100U at %p, file \"%.300U\", line %d>",
355 co->co_name, co, co->co_filename, lineno);
356 } else {
357 return PyUnicode_FromFormat(
358 "<code object %.100U at %p, file ???, line %d>",
359 co->co_name, co, lineno);
360 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361}
362
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000363static PyObject *
364code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyCodeObject *co, *cp;
367 int eq;
368 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 if ((op != Py_EQ && op != Py_NE) ||
371 !PyCode_Check(self) ||
372 !PyCode_Check(other)) {
373 Py_INCREF(Py_NotImplemented);
374 return Py_NotImplemented;
375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 co = (PyCodeObject *)self;
378 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
381 if (eq <= 0) goto unequal;
382 eq = co->co_argcount == cp->co_argcount;
383 if (!eq) goto unequal;
384 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
385 if (!eq) goto unequal;
386 eq = co->co_nlocals == cp->co_nlocals;
387 if (!eq) goto unequal;
388 eq = co->co_flags == cp->co_flags;
389 if (!eq) goto unequal;
390 eq = co->co_firstlineno == cp->co_firstlineno;
391 if (!eq) goto unequal;
392 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
393 if (eq <= 0) goto unequal;
394 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
395 if (eq <= 0) goto unequal;
396 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
397 if (eq <= 0) goto unequal;
398 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
399 if (eq <= 0) goto unequal;
400 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
401 if (eq <= 0) goto unequal;
402 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
403 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (op == Py_EQ)
406 res = Py_True;
407 else
408 res = Py_False;
409 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000410
411 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (eq < 0)
413 return NULL;
414 if (op == Py_NE)
415 res = Py_True;
416 else
417 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000418
419 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 Py_INCREF(res);
421 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422}
423
424static long
425code_hash(PyCodeObject *co)
426{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 long h, h0, h1, h2, h3, h4, h5, h6;
428 h0 = PyObject_Hash(co->co_name);
429 if (h0 == -1) return -1;
430 h1 = PyObject_Hash(co->co_code);
431 if (h1 == -1) return -1;
432 h2 = PyObject_Hash(co->co_consts);
433 if (h2 == -1) return -1;
434 h3 = PyObject_Hash(co->co_names);
435 if (h3 == -1) return -1;
436 h4 = PyObject_Hash(co->co_varnames);
437 if (h4 == -1) return -1;
438 h5 = PyObject_Hash(co->co_freevars);
439 if (h5 == -1) return -1;
440 h6 = PyObject_Hash(co->co_cellvars);
441 if (h6 == -1) return -1;
442 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
443 co->co_argcount ^ co->co_kwonlyargcount ^
444 co->co_nlocals ^ co->co_flags;
445 if (h == -1) h = -2;
446 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447}
448
449/* XXX code objects need to participate in GC? */
450
451PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyVarObject_HEAD_INIT(&PyType_Type, 0)
453 "code",
454 sizeof(PyCodeObject),
455 0,
456 (destructor)code_dealloc, /* tp_dealloc */
457 0, /* tp_print */
458 0, /* tp_getattr */
459 0, /* tp_setattr */
460 0, /* tp_reserved */
461 (reprfunc)code_repr, /* tp_repr */
462 0, /* tp_as_number */
463 0, /* tp_as_sequence */
464 0, /* tp_as_mapping */
465 (hashfunc)code_hash, /* tp_hash */
466 0, /* tp_call */
467 0, /* tp_str */
468 PyObject_GenericGetAttr, /* tp_getattro */
469 0, /* tp_setattro */
470 0, /* tp_as_buffer */
471 Py_TPFLAGS_DEFAULT, /* tp_flags */
472 code_doc, /* tp_doc */
473 0, /* tp_traverse */
474 0, /* tp_clear */
475 code_richcompare, /* tp_richcompare */
476 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
477 0, /* tp_iter */
478 0, /* tp_iternext */
479 0, /* tp_methods */
480 code_memberlist, /* tp_members */
481 0, /* tp_getset */
482 0, /* tp_base */
483 0, /* tp_dict */
484 0, /* tp_descr_get */
485 0, /* tp_descr_set */
486 0, /* tp_dictoffset */
487 0, /* tp_init */
488 0, /* tp_alloc */
489 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490};
491
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000492/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
493 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494*/
495
496int
497PyCode_Addr2Line(PyCodeObject *co, int addrq)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int size = PyBytes_Size(co->co_lnotab) / 2;
500 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
501 int line = co->co_firstlineno;
502 int addr = 0;
503 while (--size >= 0) {
504 addr += *p++;
505 if (addr > addrq)
506 break;
507 line += *p++;
508 }
509 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000512/* Update *bounds to describe the first and one-past-the-last instructions in
513 the same line as lasti. Return the number of that line. */
514int
515_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 int size, addr, line;
518 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
521 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 addr = 0;
524 line = co->co_firstlineno;
525 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 /* possible optimization: if f->f_lasti == instr_ub
528 (likely to be a common case) then we already know
529 instr_lb -- if we stored the matching value of p
530 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 /* See lnotab_notes.txt for the description of
533 co_lnotab. A point to remember: increments to p
534 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 bounds->ap_lower = 0;
537 while (size > 0) {
538 if (addr + *p > lasti)
539 break;
540 addr += *p++;
541 if (*p)
542 bounds->ap_lower = addr;
543 line += *p++;
544 --size;
545 }
546
547 if (size > 0) {
548 while (--size >= 0) {
549 addr += *p++;
550 if (*p++)
551 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 bounds->ap_upper = addr;
554 }
555 else {
556 bounds->ap_upper = INT_MAX;
557 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560}