blob: 470bf561500b408587013ab5c1b55df9245f05fe [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
Victor Stinnerc39211f2010-09-29 16:35:47 +00008PyObject *_Py_code_object_list = NULL;
9
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
11
12static int
Guido van Rossum98297ee2007-11-06 21:34:58 +000013all_name_chars(Py_UNICODE *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 static char ok_name_char[256];
16 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 if (ok_name_char[*name_chars] == 0) {
19 unsigned char *p;
20 for (p = name_chars; *p; p++)
21 ok_name_char[*p] = 1;
22 }
23 while (*s) {
24 if (*s >= 128)
25 return 0;
26 if (ok_name_char[*s++] == 0)
27 return 0;
28 }
29 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030}
31
32static void
33intern_strings(PyObject *tuple)
34{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
38 PyObject *v = PyTuple_GET_ITEM(tuple, i);
39 if (v == NULL || !PyUnicode_CheckExact(v)) {
40 Py_FatalError("non-string found in code slot");
41 }
42 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
43 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044}
45
46
47PyCodeObject *
Guido van Rossum4f72a782006-10-27 23:31:49 +000048PyCode_New(int argcount, int kwonlyargcount,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int nlocals, int stacksize, int flags,
50 PyObject *code, PyObject *consts, PyObject *names,
51 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
52 PyObject *filename, PyObject *name, int firstlineno,
53 PyObject *lnotab)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 PyCodeObject *co;
56 Py_ssize_t i;
Guido van Rossum00bc0e02007-10-15 02:52:41 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 /* Check argument types */
59 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
60 code == NULL ||
61 consts == NULL || !PyTuple_Check(consts) ||
62 names == NULL || !PyTuple_Check(names) ||
63 varnames == NULL || !PyTuple_Check(varnames) ||
64 freevars == NULL || !PyTuple_Check(freevars) ||
65 cellvars == NULL || !PyTuple_Check(cellvars) ||
66 name == NULL || !PyUnicode_Check(name) ||
67 filename == NULL || !PyUnicode_Check(filename) ||
68 lnotab == NULL || !PyBytes_Check(lnotab) ||
69 !PyObject_CheckReadBuffer(code)) {
70 PyErr_BadInternalCall();
71 return NULL;
72 }
73 intern_strings(names);
74 intern_strings(varnames);
75 intern_strings(freevars);
76 intern_strings(cellvars);
77 /* Intern selected string constants */
78 for (i = PyTuple_Size(consts); --i >= 0; ) {
79 PyObject *v = PyTuple_GetItem(consts, i);
80 if (!PyUnicode_Check(v))
81 continue;
82 if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
83 continue;
84 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
85 }
86 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
87 if (co != NULL) {
88 co->co_argcount = argcount;
89 co->co_kwonlyargcount = kwonlyargcount;
90 co->co_nlocals = nlocals;
91 co->co_stacksize = stacksize;
92 co->co_flags = flags;
93 Py_INCREF(code);
94 co->co_code = code;
95 Py_INCREF(consts);
96 co->co_consts = consts;
97 Py_INCREF(names);
98 co->co_names = names;
99 Py_INCREF(varnames);
100 co->co_varnames = varnames;
101 Py_INCREF(freevars);
102 co->co_freevars = freevars;
103 Py_INCREF(cellvars);
104 co->co_cellvars = cellvars;
105 Py_INCREF(filename);
106 co->co_filename = filename;
107 Py_INCREF(name);
108 co->co_name = name;
109 co->co_firstlineno = firstlineno;
110 Py_INCREF(lnotab);
111 co->co_lnotab = lnotab;
112 co->co_zombieframe = NULL;
113 co->co_weakreflist = NULL;
Victor Stinnerc39211f2010-09-29 16:35:47 +0000114
115 if (_Py_code_object_list != NULL) {
116 int err;
117 PyObject *ref = PyWeakref_NewRef((PyObject*)co, NULL);
118 if (ref == NULL)
119 goto error;
120 err = PyList_Append(_Py_code_object_list, ref);
121 Py_DECREF(ref);
122 if (err)
123 goto error;
124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 }
126 return co;
Victor Stinnerc39211f2010-09-29 16:35:47 +0000127
128error:
129 Py_DECREF(co);
130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131}
132
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000133PyCodeObject *
134PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 static PyObject *emptystring = NULL;
137 static PyObject *nulltuple = NULL;
138 PyObject *filename_ob = NULL;
139 PyObject *funcname_ob = NULL;
140 PyCodeObject *result = NULL;
141 if (emptystring == NULL) {
142 emptystring = PyBytes_FromString("");
143 if (emptystring == NULL)
144 goto failed;
145 }
146 if (nulltuple == NULL) {
147 nulltuple = PyTuple_New(0);
148 if (nulltuple == NULL)
149 goto failed;
150 }
151 funcname_ob = PyUnicode_FromString(funcname);
152 if (funcname_ob == NULL)
153 goto failed;
154 filename_ob = PyUnicode_DecodeFSDefault(filename);
155 if (filename_ob == NULL)
156 goto failed;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 result = PyCode_New(0, /* argcount */
159 0, /* kwonlyargcount */
160 0, /* nlocals */
161 0, /* stacksize */
162 0, /* flags */
163 emptystring, /* code */
164 nulltuple, /* consts */
165 nulltuple, /* names */
166 nulltuple, /* varnames */
167 nulltuple, /* freevars */
168 nulltuple, /* cellvars */
169 filename_ob, /* filename */
170 funcname_ob, /* name */
171 firstlineno, /* firstlineno */
172 emptystring /* lnotab */
173 );
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000174
175failed:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 Py_XDECREF(funcname_ob);
177 Py_XDECREF(filename_ob);
178 return result;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000179}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180
181#define OFF(x) offsetof(PyCodeObject, x)
182
183static PyMemberDef code_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
185 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
186 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
187 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
188 {"co_flags", T_INT, OFF(co_flags), READONLY},
189 {"co_code", T_OBJECT, OFF(co_code), READONLY},
190 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
191 {"co_names", T_OBJECT, OFF(co_names), READONLY},
192 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
193 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
194 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
195 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
196 {"co_name", T_OBJECT, OFF(co_name), READONLY},
197 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
198 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
199 {NULL} /* Sentinel */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200};
201
202/* Helper for code_new: return a shallow copy of a tuple that is
203 guaranteed to contain exact strings, by converting string subclasses
204 to exact strings and complaining if a non-string is found. */
205static PyObject*
206validate_and_copy_tuple(PyObject *tup)
207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyObject *newtuple;
209 PyObject *item;
210 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 len = PyTuple_GET_SIZE(tup);
213 newtuple = PyTuple_New(len);
214 if (newtuple == NULL)
215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 for (i = 0; i < len; i++) {
218 item = PyTuple_GET_ITEM(tup, i);
219 if (PyUnicode_CheckExact(item)) {
220 Py_INCREF(item);
221 }
222 else if (!PyUnicode_Check(item)) {
223 PyErr_Format(
224 PyExc_TypeError,
225 "name tuples must contain only "
226 "strings, not '%.500s'",
227 item->ob_type->tp_name);
228 Py_DECREF(newtuple);
229 return NULL;
230 }
231 else {
232 item = PyUnicode_FromUnicode(
233 PyUnicode_AS_UNICODE(item),
234 PyUnicode_GET_SIZE(item));
235 if (item == NULL) {
236 Py_DECREF(newtuple);
237 return NULL;
238 }
239 }
240 PyTuple_SET_ITEM(newtuple, i, item);
241 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return newtuple;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244}
245
246PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000247"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000248 constants, names, varnames, filename, name, firstlineno,\n\
249 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250\n\
251Create a code object. Not for the faint of heart.");
252
253static PyObject *
254code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 int argcount;
257 int kwonlyargcount;
258 int nlocals;
259 int stacksize;
260 int flags;
261 PyObject *co = NULL;
262 PyObject *code;
263 PyObject *consts;
264 PyObject *names, *ournames = NULL;
265 PyObject *varnames, *ourvarnames = NULL;
266 PyObject *freevars = NULL, *ourfreevars = NULL;
267 PyObject *cellvars = NULL, *ourcellvars = NULL;
268 PyObject *filename;
269 PyObject *name;
270 int firstlineno;
271 PyObject *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
274 &argcount, &kwonlyargcount,
275 &nlocals, &stacksize, &flags,
276 &code,
277 &PyTuple_Type, &consts,
278 &PyTuple_Type, &names,
279 &PyTuple_Type, &varnames,
280 &filename, &name,
281 &firstlineno, &lnotab,
282 &PyTuple_Type, &freevars,
283 &PyTuple_Type, &cellvars))
284 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 if (argcount < 0) {
287 PyErr_SetString(
288 PyExc_ValueError,
289 "code: argcount must not be negative");
290 goto cleanup;
291 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 if (kwonlyargcount < 0) {
294 PyErr_SetString(
295 PyExc_ValueError,
296 "code: kwonlyargcount must not be negative");
297 goto cleanup;
298 }
299 if (nlocals < 0) {
300 PyErr_SetString(
301 PyExc_ValueError,
302 "code: nlocals must not be negative");
303 goto cleanup;
304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 ournames = validate_and_copy_tuple(names);
307 if (ournames == NULL)
308 goto cleanup;
309 ourvarnames = validate_and_copy_tuple(varnames);
310 if (ourvarnames == NULL)
311 goto cleanup;
312 if (freevars)
313 ourfreevars = validate_and_copy_tuple(freevars);
314 else
315 ourfreevars = PyTuple_New(0);
316 if (ourfreevars == NULL)
317 goto cleanup;
318 if (cellvars)
319 ourcellvars = validate_and_copy_tuple(cellvars);
320 else
321 ourcellvars = PyTuple_New(0);
322 if (ourcellvars == NULL)
323 goto cleanup;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
326 nlocals, stacksize, flags,
327 code, consts, ournames, ourvarnames,
328 ourfreevars, ourcellvars, filename,
329 name, firstlineno, lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_XDECREF(ournames);
332 Py_XDECREF(ourvarnames);
333 Py_XDECREF(ourfreevars);
334 Py_XDECREF(ourcellvars);
335 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336}
337
338static void
339code_dealloc(PyCodeObject *co)
340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 Py_XDECREF(co->co_code);
342 Py_XDECREF(co->co_consts);
343 Py_XDECREF(co->co_names);
344 Py_XDECREF(co->co_varnames);
345 Py_XDECREF(co->co_freevars);
346 Py_XDECREF(co->co_cellvars);
347 Py_XDECREF(co->co_filename);
348 Py_XDECREF(co->co_name);
349 Py_XDECREF(co->co_lnotab);
350 if (co->co_zombieframe != NULL)
351 PyObject_GC_Del(co->co_zombieframe);
352 if (co->co_weakreflist != NULL)
353 PyObject_ClearWeakRefs((PyObject*)co);
354 PyObject_DEL(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355}
356
357static PyObject *
358code_repr(PyCodeObject *co)
359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int lineno;
361 if (co->co_firstlineno != 0)
362 lineno = co->co_firstlineno;
363 else
364 lineno = -1;
365 if (co->co_filename && PyUnicode_Check(co->co_filename)) {
366 return PyUnicode_FromFormat(
367 "<code object %.100U at %p, file \"%.300U\", line %d>",
368 co->co_name, co, co->co_filename, lineno);
369 } else {
370 return PyUnicode_FromFormat(
371 "<code object %.100U at %p, file ???, line %d>",
372 co->co_name, co, lineno);
373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374}
375
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000376static PyObject *
377code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyCodeObject *co, *cp;
380 int eq;
381 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if ((op != Py_EQ && op != Py_NE) ||
384 !PyCode_Check(self) ||
385 !PyCode_Check(other)) {
386 Py_INCREF(Py_NotImplemented);
387 return Py_NotImplemented;
388 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 co = (PyCodeObject *)self;
391 cp = (PyCodeObject *)other;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
394 if (eq <= 0) goto unequal;
395 eq = co->co_argcount == cp->co_argcount;
396 if (!eq) goto unequal;
397 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
398 if (!eq) goto unequal;
399 eq = co->co_nlocals == cp->co_nlocals;
400 if (!eq) goto unequal;
401 eq = co->co_flags == cp->co_flags;
402 if (!eq) goto unequal;
403 eq = co->co_firstlineno == cp->co_firstlineno;
404 if (!eq) goto unequal;
405 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
406 if (eq <= 0) goto unequal;
407 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
408 if (eq <= 0) goto unequal;
409 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
410 if (eq <= 0) goto unequal;
411 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
412 if (eq <= 0) goto unequal;
413 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
414 if (eq <= 0) goto unequal;
415 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
416 if (eq <= 0) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (op == Py_EQ)
419 res = Py_True;
420 else
421 res = Py_False;
422 goto done;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000423
424 unequal:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (eq < 0)
426 return NULL;
427 if (op == Py_NE)
428 res = Py_True;
429 else
430 res = Py_False;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000431
432 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 Py_INCREF(res);
434 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435}
436
437static long
438code_hash(PyCodeObject *co)
439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 long h, h0, h1, h2, h3, h4, h5, h6;
441 h0 = PyObject_Hash(co->co_name);
442 if (h0 == -1) return -1;
443 h1 = PyObject_Hash(co->co_code);
444 if (h1 == -1) return -1;
445 h2 = PyObject_Hash(co->co_consts);
446 if (h2 == -1) return -1;
447 h3 = PyObject_Hash(co->co_names);
448 if (h3 == -1) return -1;
449 h4 = PyObject_Hash(co->co_varnames);
450 if (h4 == -1) return -1;
451 h5 = PyObject_Hash(co->co_freevars);
452 if (h5 == -1) return -1;
453 h6 = PyObject_Hash(co->co_cellvars);
454 if (h6 == -1) return -1;
455 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
456 co->co_argcount ^ co->co_kwonlyargcount ^
457 co->co_nlocals ^ co->co_flags;
458 if (h == -1) h = -2;
459 return h;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460}
461
462/* XXX code objects need to participate in GC? */
463
464PyTypeObject PyCode_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 PyVarObject_HEAD_INIT(&PyType_Type, 0)
466 "code",
467 sizeof(PyCodeObject),
468 0,
469 (destructor)code_dealloc, /* tp_dealloc */
470 0, /* tp_print */
471 0, /* tp_getattr */
472 0, /* tp_setattr */
473 0, /* tp_reserved */
474 (reprfunc)code_repr, /* tp_repr */
475 0, /* tp_as_number */
476 0, /* tp_as_sequence */
477 0, /* tp_as_mapping */
478 (hashfunc)code_hash, /* tp_hash */
479 0, /* tp_call */
480 0, /* tp_str */
481 PyObject_GenericGetAttr, /* tp_getattro */
482 0, /* tp_setattro */
483 0, /* tp_as_buffer */
484 Py_TPFLAGS_DEFAULT, /* tp_flags */
485 code_doc, /* tp_doc */
486 0, /* tp_traverse */
487 0, /* tp_clear */
488 code_richcompare, /* tp_richcompare */
489 offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
490 0, /* tp_iter */
491 0, /* tp_iternext */
492 0, /* tp_methods */
493 code_memberlist, /* tp_members */
494 0, /* tp_getset */
495 0, /* tp_base */
496 0, /* tp_dict */
497 0, /* tp_descr_get */
498 0, /* tp_descr_set */
499 0, /* tp_dictoffset */
500 0, /* tp_init */
501 0, /* tp_alloc */
502 code_new, /* tp_new */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503};
504
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000505/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
506 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507*/
508
509int
510PyCode_Addr2Line(PyCodeObject *co, int addrq)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 int size = PyBytes_Size(co->co_lnotab) / 2;
513 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
514 int line = co->co_firstlineno;
515 int addr = 0;
516 while (--size >= 0) {
517 addr += *p++;
518 if (addr > addrq)
519 break;
520 line += *p++;
521 }
522 return line;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000525/* Update *bounds to describe the first and one-past-the-last instructions in
526 the same line as lasti. Return the number of that line. */
527int
528_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 int size, addr, line;
531 unsigned char* p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
534 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 addr = 0;
537 line = co->co_firstlineno;
538 assert(line > 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 /* possible optimization: if f->f_lasti == instr_ub
541 (likely to be a common case) then we already know
542 instr_lb -- if we stored the matching value of p
543 somwhere we could skip the first while loop. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 /* See lnotab_notes.txt for the description of
546 co_lnotab. A point to remember: increments to p
547 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 bounds->ap_lower = 0;
550 while (size > 0) {
551 if (addr + *p > lasti)
552 break;
553 addr += *p++;
554 if (*p)
555 bounds->ap_lower = addr;
556 line += *p++;
557 --size;
558 }
559
560 if (size > 0) {
561 while (--size >= 0) {
562 addr += *p++;
563 if (*p++)
564 break;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 bounds->ap_upper = addr;
567 }
568 else {
569 bounds->ap_upper = INT_MAX;
570 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 return line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000573}