blob: a772c56a5b737a87e97c6b8ab52cf8df8ac8ebe2 [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 */
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +000057 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058 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) ||
Christian Heimes72b710a2008-05-26 13:28:38 +000066 lnotab == NULL || !PyBytes_Check(lnotab) ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000067 !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
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000115PyCodeObject *
116PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
117{
118 static PyObject *emptystring = NULL;
119 static PyObject *nulltuple = NULL;
120 PyObject *filename_ob = NULL;
121 PyObject *funcname_ob = NULL;
122 PyCodeObject *result = NULL;
123 if (emptystring == NULL) {
124 emptystring = PyBytes_FromString("");
125 if (emptystring == NULL)
126 goto failed;
127 }
128 if (nulltuple == NULL) {
129 nulltuple = PyTuple_New(0);
130 if (nulltuple == NULL)
131 goto failed;
132 }
133 funcname_ob = PyUnicode_FromString(funcname);
134 if (funcname_ob == NULL)
135 goto failed;
136 filename_ob = PyUnicode_DecodeFSDefault(filename);
137 if (filename_ob == NULL)
138 goto failed;
139
140 result = PyCode_New(0, /* argcount */
141 0, /* kwonlyargcount */
142 0, /* nlocals */
143 0, /* stacksize */
144 0, /* flags */
145 emptystring, /* code */
146 nulltuple, /* consts */
147 nulltuple, /* names */
148 nulltuple, /* varnames */
149 nulltuple, /* freevars */
150 nulltuple, /* cellvars */
151 filename_ob, /* filename */
152 funcname_ob, /* name */
153 firstlineno, /* firstlineno */
154 emptystring /* lnotab */
155 );
156
157failed:
158 Py_XDECREF(funcname_ob);
159 Py_XDECREF(filename_ob);
160 return result;
161}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163#define OFF(x) offsetof(PyCodeObject, x)
164
165static PyMemberDef code_memberlist[] = {
166 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
Guido van Rossum4f72a782006-10-27 23:31:49 +0000167 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
169 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
170 {"co_flags", T_INT, OFF(co_flags), READONLY},
171 {"co_code", T_OBJECT, OFF(co_code), READONLY},
172 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
173 {"co_names", T_OBJECT, OFF(co_names), READONLY},
174 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
175 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
176 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
177 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
178 {"co_name", T_OBJECT, OFF(co_name), READONLY},
179 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
180 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
181 {NULL} /* Sentinel */
182};
183
184/* Helper for code_new: return a shallow copy of a tuple that is
185 guaranteed to contain exact strings, by converting string subclasses
186 to exact strings and complaining if a non-string is found. */
187static PyObject*
188validate_and_copy_tuple(PyObject *tup)
189{
190 PyObject *newtuple;
191 PyObject *item;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192 Py_ssize_t i, len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194 len = PyTuple_GET_SIZE(tup);
195 newtuple = PyTuple_New(len);
196 if (newtuple == NULL)
197 return NULL;
198
199 for (i = 0; i < len; i++) {
200 item = PyTuple_GET_ITEM(tup, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000201 if (PyUnicode_CheckExact(item)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 Py_INCREF(item);
203 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000204 else if (!PyUnicode_Check(item)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205 PyErr_Format(
206 PyExc_TypeError,
207 "name tuples must contain only "
208 "strings, not '%.500s'",
209 item->ob_type->tp_name);
210 Py_DECREF(newtuple);
211 return NULL;
212 }
213 else {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000214 item = PyUnicode_FromUnicode(
215 PyUnicode_AS_UNICODE(item),
216 PyUnicode_GET_SIZE(item));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 if (item == NULL) {
218 Py_DECREF(newtuple);
219 return NULL;
220 }
221 }
222 PyTuple_SET_ITEM(newtuple, i, item);
223 }
224
225 return newtuple;
226}
227
228PyDoc_STRVAR(code_doc,
Georg Brandl8c1a50a2009-07-18 09:07:48 +0000229"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
Georg Brandla1e7e132008-01-26 09:39:23 +0000230 constants, names, varnames, filename, name, firstlineno,\n\
231 lnotab[, freevars[, cellvars]])\n\
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232\n\
233Create a code object. Not for the faint of heart.");
234
235static PyObject *
236code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
237{
238 int argcount;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000239 int kwonlyargcount;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 int nlocals;
241 int stacksize;
242 int flags;
243 PyObject *co = NULL;
244 PyObject *code;
245 PyObject *consts;
246 PyObject *names, *ournames = NULL;
247 PyObject *varnames, *ourvarnames = NULL;
248 PyObject *freevars = NULL, *ourfreevars = NULL;
249 PyObject *cellvars = NULL, *ourcellvars = NULL;
250 PyObject *filename;
251 PyObject *name;
252 int firstlineno;
253 PyObject *lnotab;
254
Guido van Rossum98297ee2007-11-06 21:34:58 +0000255 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000256 &argcount, &kwonlyargcount,
257 &nlocals, &stacksize, &flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 &code,
259 &PyTuple_Type, &consts,
260 &PyTuple_Type, &names,
261 &PyTuple_Type, &varnames,
262 &filename, &name,
263 &firstlineno, &lnotab,
264 &PyTuple_Type, &freevars,
265 &PyTuple_Type, &cellvars))
266 return NULL;
267
268 if (argcount < 0) {
269 PyErr_SetString(
270 PyExc_ValueError,
271 "code: argcount must not be negative");
272 goto cleanup;
273 }
274
Guido van Rossum4f72a782006-10-27 23:31:49 +0000275 if (kwonlyargcount < 0) {
276 PyErr_SetString(
277 PyExc_ValueError,
278 "code: kwonlyargcount must not be negative");
279 goto cleanup;
280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 if (nlocals < 0) {
282 PyErr_SetString(
283 PyExc_ValueError,
284 "code: nlocals must not be negative");
285 goto cleanup;
286 }
287
288 ournames = validate_and_copy_tuple(names);
289 if (ournames == NULL)
290 goto cleanup;
291 ourvarnames = validate_and_copy_tuple(varnames);
292 if (ourvarnames == NULL)
293 goto cleanup;
294 if (freevars)
295 ourfreevars = validate_and_copy_tuple(freevars);
296 else
297 ourfreevars = PyTuple_New(0);
298 if (ourfreevars == NULL)
299 goto cleanup;
300 if (cellvars)
301 ourcellvars = validate_and_copy_tuple(cellvars);
302 else
303 ourcellvars = PyTuple_New(0);
304 if (ourcellvars == NULL)
305 goto cleanup;
306
Guido van Rossum4f72a782006-10-27 23:31:49 +0000307 co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
308 nlocals, stacksize, flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309 code, consts, ournames, ourvarnames,
310 ourfreevars, ourcellvars, filename,
311 name, firstlineno, lnotab);
312 cleanup:
313 Py_XDECREF(ournames);
314 Py_XDECREF(ourvarnames);
315 Py_XDECREF(ourfreevars);
316 Py_XDECREF(ourcellvars);
317 return co;
318}
319
320static void
321code_dealloc(PyCodeObject *co)
322{
323 Py_XDECREF(co->co_code);
324 Py_XDECREF(co->co_consts);
325 Py_XDECREF(co->co_names);
326 Py_XDECREF(co->co_varnames);
327 Py_XDECREF(co->co_freevars);
328 Py_XDECREF(co->co_cellvars);
329 Py_XDECREF(co->co_filename);
330 Py_XDECREF(co->co_name);
331 Py_XDECREF(co->co_lnotab);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000332 if (co->co_zombieframe != NULL)
333 PyObject_GC_Del(co->co_zombieframe);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 PyObject_DEL(co);
335}
336
337static PyObject *
338code_repr(PyCodeObject *co)
339{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 int lineno = -1;
341 char *filename = "???";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 if (co->co_firstlineno != 0)
344 lineno = co->co_firstlineno;
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000345 if (co->co_filename && PyUnicode_Check(co->co_filename))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000346 filename = _PyUnicode_AsString(co->co_filename);
Walter Dörwald933daed2007-06-06 15:15:34 +0000347 return PyUnicode_FromFormat(
Neal Norwitz41103bf2007-08-24 23:12:06 +0000348 "<code object %.100U at %p, file \"%.300s\", line %d>",
349 co->co_name, co, filename, lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350}
351
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000352static PyObject *
353code_richcompare(PyObject *self, PyObject *other, int op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354{
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000355 PyCodeObject *co, *cp;
356 int eq;
357 PyObject *res;
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000358
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000359 if ((op != Py_EQ && op != Py_NE) ||
360 !PyCode_Check(self) ||
361 !PyCode_Check(other)) {
362 Py_INCREF(Py_NotImplemented);
363 return Py_NotImplemented;
364 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000366 co = (PyCodeObject *)self;
367 cp = (PyCodeObject *)other;
368
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000369 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000370 if (eq <= 0) goto unequal;
371 eq = co->co_argcount == cp->co_argcount;
372 if (!eq) goto unequal;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000373 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
374 if (!eq) goto unequal;
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000375 eq = co->co_nlocals == cp->co_nlocals;
376 if (!eq) goto unequal;
377 eq = co->co_flags == cp->co_flags;
378 if (!eq) goto unequal;
379 eq = co->co_firstlineno == cp->co_firstlineno;
380 if (!eq) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000381 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000382 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000383 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000384 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000385 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000386 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000387 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000388 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000389 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000390 if (eq <= 0) goto unequal;
Guido van Rossumf1624cd2006-08-24 23:43:52 +0000391 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000392 if (eq <= 0) goto unequal;
393
394 if (op == Py_EQ)
395 res = Py_True;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 else
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000397 res = Py_False;
398 goto done;
399
400 unequal:
401 if (eq < 0)
402 return NULL;
403 if (op == Py_NE)
404 res = Py_True;
405 else
406 res = Py_False;
407
408 done:
409 Py_INCREF(res);
410 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411}
412
413static long
414code_hash(PyCodeObject *co)
415{
416 long h, h0, h1, h2, h3, h4, h5, h6;
417 h0 = PyObject_Hash(co->co_name);
418 if (h0 == -1) return -1;
419 h1 = PyObject_Hash(co->co_code);
420 if (h1 == -1) return -1;
421 h2 = PyObject_Hash(co->co_consts);
422 if (h2 == -1) return -1;
423 h3 = PyObject_Hash(co->co_names);
424 if (h3 == -1) return -1;
425 h4 = PyObject_Hash(co->co_varnames);
426 if (h4 == -1) return -1;
427 h5 = PyObject_Hash(co->co_freevars);
428 if (h5 == -1) return -1;
429 h6 = PyObject_Hash(co->co_cellvars);
430 if (h6 == -1) return -1;
431 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum4f72a782006-10-27 23:31:49 +0000432 co->co_argcount ^ co->co_kwonlyargcount ^
433 co->co_nlocals ^ co->co_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 if (h == -1) h = -2;
435 return h;
436}
437
438/* XXX code objects need to participate in GC? */
439
440PyTypeObject PyCode_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000441 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 "code",
443 sizeof(PyCodeObject),
444 0,
445 (destructor)code_dealloc, /* tp_dealloc */
446 0, /* tp_print */
447 0, /* tp_getattr */
448 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000449 0, /* tp_reserved */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 (reprfunc)code_repr, /* tp_repr */
451 0, /* tp_as_number */
452 0, /* tp_as_sequence */
453 0, /* tp_as_mapping */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000454 (hashfunc)code_hash, /* tp_hash */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 0, /* tp_call */
456 0, /* tp_str */
457 PyObject_GenericGetAttr, /* tp_getattro */
458 0, /* tp_setattro */
459 0, /* tp_as_buffer */
460 Py_TPFLAGS_DEFAULT, /* tp_flags */
461 code_doc, /* tp_doc */
462 0, /* tp_traverse */
463 0, /* tp_clear */
Guido van Rossumb6bb0c72006-08-24 04:12:18 +0000464 code_richcompare, /* tp_richcompare */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465 0, /* tp_weaklistoffset */
466 0, /* tp_iter */
467 0, /* tp_iternext */
468 0, /* tp_methods */
469 code_memberlist, /* tp_members */
470 0, /* tp_getset */
471 0, /* tp_base */
472 0, /* tp_dict */
473 0, /* tp_descr_get */
474 0, /* tp_descr_set */
475 0, /* tp_dictoffset */
476 0, /* tp_init */
477 0, /* tp_alloc */
478 code_new, /* tp_new */
479};
480
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000481/* Use co_lnotab to compute the line number from a bytecode index, addrq. See
482 lnotab_notes.txt for the details of the lnotab representation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483*/
484
485int
486PyCode_Addr2Line(PyCodeObject *co, int addrq)
487{
Christian Heimes72b710a2008-05-26 13:28:38 +0000488 int size = PyBytes_Size(co->co_lnotab) / 2;
489 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 int line = co->co_firstlineno;
491 int addr = 0;
492 while (--size >= 0) {
493 addr += *p++;
494 if (addr > addrq)
495 break;
496 line += *p++;
497 }
498 return line;
499}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000500
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000501/* Update *bounds to describe the first and one-past-the-last instructions in
502 the same line as lasti. Return the number of that line. */
503int
504_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505{
506 int size, addr, line;
507 unsigned char* p;
508
Christian Heimes72b710a2008-05-26 13:28:38 +0000509 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
510 size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511
512 addr = 0;
513 line = co->co_firstlineno;
514 assert(line > 0);
515
516 /* possible optimization: if f->f_lasti == instr_ub
517 (likely to be a common case) then we already know
518 instr_lb -- if we stored the matching value of p
519 somwhere we could skip the first while loop. */
520
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000521 /* See lnotab_notes.txt for the description of
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000522 co_lnotab. A point to remember: increments to p
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +0000523 come in (addr, line) pairs. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 bounds->ap_lower = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 while (size > 0) {
527 if (addr + *p > lasti)
528 break;
529 addr += *p++;
530 if (*p)
531 bounds->ap_lower = addr;
532 line += *p++;
533 --size;
534 }
535
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 if (size > 0) {
537 while (--size >= 0) {
538 addr += *p++;
539 if (*p++)
540 break;
541 }
542 bounds->ap_upper = addr;
543 }
544 else {
545 bounds->ap_upper = INT_MAX;
546 }
547
548 return line;
549}