~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Objects/codeobject.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#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
 
 
10
static int
 
11
all_name_chars(PyObject *o)
 
12
{
 
13
    static char ok_name_char[256];
 
14
    static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
 
15
    PyUnicodeObject *u = (PyUnicodeObject *)o;
 
16
    const unsigned char *s;
 
17
 
 
18
    if (!PyUnicode_Check(o) || PyUnicode_READY(u) == -1 ||
 
19
        PyUnicode_MAX_CHAR_VALUE(u) >= 128)
 
20
        return 0;
 
21
 
 
22
    if (ok_name_char[*name_chars] == 0) {
 
23
        unsigned char *p;
 
24
        for (p = name_chars; *p; p++)
 
25
            ok_name_char[*p] = 1;
 
26
    }
 
27
    s = PyUnicode_1BYTE_DATA(u);
 
28
    while (*s) {
 
29
        if (ok_name_char[*s++] == 0)
 
30
            return 0;
 
31
    }
 
32
    return 1;
 
33
}
 
34
 
 
35
static void
 
36
intern_strings(PyObject *tuple)
 
37
{
 
38
    Py_ssize_t i;
 
39
 
 
40
    for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
 
41
        PyObject *v = PyTuple_GET_ITEM(tuple, i);
 
42
        if (v == NULL || !PyUnicode_CheckExact(v)) {
 
43
            Py_FatalError("non-string found in code slot");
 
44
        }
 
45
        PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
 
46
    }
 
47
}
 
48
 
 
49
 
 
50
PyCodeObject *
 
51
PyCode_New(int argcount, int kwonlyargcount,
 
52
           int nlocals, int stacksize, int flags,
 
53
           PyObject *code, PyObject *consts, PyObject *names,
 
54
           PyObject *varnames, PyObject *freevars, PyObject *cellvars,
 
55
           PyObject *filename, PyObject *name, int firstlineno,
 
56
           PyObject *lnotab)
 
57
{
 
58
    PyCodeObject *co;
 
59
    unsigned char *cell2arg = NULL;
 
60
    Py_ssize_t i, n_cellvars;
 
61
 
 
62
    /* Check argument types */
 
63
    if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 ||
 
64
        code == NULL ||
 
65
        consts == NULL || !PyTuple_Check(consts) ||
 
66
        names == NULL || !PyTuple_Check(names) ||
 
67
        varnames == NULL || !PyTuple_Check(varnames) ||
 
68
        freevars == NULL || !PyTuple_Check(freevars) ||
 
69
        cellvars == NULL || !PyTuple_Check(cellvars) ||
 
70
        name == NULL || !PyUnicode_Check(name) ||
 
71
        filename == NULL || !PyUnicode_Check(filename) ||
 
72
        lnotab == NULL || !PyBytes_Check(lnotab) ||
 
73
        !PyObject_CheckReadBuffer(code)) {
 
74
        PyErr_BadInternalCall();
 
75
        return NULL;
 
76
    }
 
77
 
 
78
    /* Ensure that the filename is a ready Unicode string */
 
79
    if (PyUnicode_READY(filename) < 0)
 
80
        return NULL;
 
81
 
 
82
    n_cellvars = PyTuple_GET_SIZE(cellvars);
 
83
    intern_strings(names);
 
84
    intern_strings(varnames);
 
85
    intern_strings(freevars);
 
86
    intern_strings(cellvars);
 
87
    /* Intern selected string constants */
 
88
    for (i = PyTuple_GET_SIZE(consts); --i >= 0; ) {
 
89
        PyObject *v = PyTuple_GetItem(consts, i);
 
90
        if (!all_name_chars(v))
 
91
            continue;
 
92
        PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
 
93
    }
 
94
    /* Create mapping between cells and arguments if needed. */
 
95
    if (n_cellvars) {
 
96
        Py_ssize_t total_args = argcount + kwonlyargcount +
 
97
            ((flags & CO_VARARGS) != 0) + ((flags & CO_VARKEYWORDS) != 0);
 
98
        Py_ssize_t alloc_size = sizeof(unsigned char) * n_cellvars;
 
99
        int used_cell2arg = 0;
 
100
        cell2arg = PyMem_MALLOC(alloc_size);
 
101
        if (cell2arg == NULL)
 
102
            return NULL;
 
103
        memset(cell2arg, CO_CELL_NOT_AN_ARG, alloc_size);
 
104
        /* Find cells which are also arguments. */
 
105
        for (i = 0; i < n_cellvars; i++) {
 
106
            Py_ssize_t j;
 
107
            PyObject *cell = PyTuple_GET_ITEM(cellvars, i);
 
108
            for (j = 0; j < total_args; j++) {
 
109
                PyObject *arg = PyTuple_GET_ITEM(varnames, j);
 
110
                if (!PyUnicode_Compare(cell, arg)) {
 
111
                    cell2arg[i] = j;
 
112
                    used_cell2arg = 1;
 
113
                    break;
 
114
                }
 
115
            }
 
116
        }
 
117
        if (!used_cell2arg) {
 
118
            PyMem_FREE(cell2arg);
 
119
            cell2arg = NULL;
 
120
        }
 
121
    }
 
122
    co = PyObject_NEW(PyCodeObject, &PyCode_Type);
 
123
    if (co == NULL) {
 
124
        if (cell2arg)
 
125
            PyMem_FREE(cell2arg);
 
126
        return NULL;
 
127
    }
 
128
    co->co_argcount = argcount;
 
129
    co->co_kwonlyargcount = kwonlyargcount;
 
130
    co->co_nlocals = nlocals;
 
131
    co->co_stacksize = stacksize;
 
132
    co->co_flags = flags;
 
133
    Py_INCREF(code);
 
134
    co->co_code = code;
 
135
    Py_INCREF(consts);
 
136
    co->co_consts = consts;
 
137
    Py_INCREF(names);
 
138
    co->co_names = names;
 
139
    Py_INCREF(varnames);
 
140
    co->co_varnames = varnames;
 
141
    Py_INCREF(freevars);
 
142
    co->co_freevars = freevars;
 
143
    Py_INCREF(cellvars);
 
144
    co->co_cellvars = cellvars;
 
145
    co->co_cell2arg = cell2arg;
 
146
    Py_INCREF(filename);
 
147
    co->co_filename = filename;
 
148
    Py_INCREF(name);
 
149
    co->co_name = name;
 
150
    co->co_firstlineno = firstlineno;
 
151
    Py_INCREF(lnotab);
 
152
    co->co_lnotab = lnotab;
 
153
    co->co_zombieframe = NULL;
 
154
    co->co_weakreflist = NULL;
 
155
    return co;
 
156
}
 
157
 
 
158
PyCodeObject *
 
159
PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
 
160
{
 
161
    static PyObject *emptystring = NULL;
 
162
    static PyObject *nulltuple = NULL;
 
163
    PyObject *filename_ob = NULL;
 
164
    PyObject *funcname_ob = NULL;
 
165
    PyCodeObject *result = NULL;
 
166
    if (emptystring == NULL) {
 
167
        emptystring = PyBytes_FromString("");
 
168
        if (emptystring == NULL)
 
169
            goto failed;
 
170
    }
 
171
    if (nulltuple == NULL) {
 
172
        nulltuple = PyTuple_New(0);
 
173
        if (nulltuple == NULL)
 
174
            goto failed;
 
175
    }
 
176
    funcname_ob = PyUnicode_FromString(funcname);
 
177
    if (funcname_ob == NULL)
 
178
        goto failed;
 
179
    filename_ob = PyUnicode_DecodeFSDefault(filename);
 
180
    if (filename_ob == NULL)
 
181
        goto failed;
 
182
 
 
183
    result = PyCode_New(0,                      /* argcount */
 
184
                0,                              /* kwonlyargcount */
 
185
                0,                              /* nlocals */
 
186
                0,                              /* stacksize */
 
187
                0,                              /* flags */
 
188
                emptystring,                    /* code */
 
189
                nulltuple,                      /* consts */
 
190
                nulltuple,                      /* names */
 
191
                nulltuple,                      /* varnames */
 
192
                nulltuple,                      /* freevars */
 
193
                nulltuple,                      /* cellvars */
 
194
                filename_ob,                    /* filename */
 
195
                funcname_ob,                    /* name */
 
196
                firstlineno,                    /* firstlineno */
 
197
                emptystring                     /* lnotab */
 
198
                );
 
199
 
 
200
failed:
 
201
    Py_XDECREF(funcname_ob);
 
202
    Py_XDECREF(filename_ob);
 
203
    return result;
 
204
}
 
205
 
 
206
#define OFF(x) offsetof(PyCodeObject, x)
 
207
 
 
208
static PyMemberDef code_memberlist[] = {
 
209
    {"co_argcount",     T_INT,          OFF(co_argcount),       READONLY},
 
210
    {"co_kwonlyargcount",       T_INT,  OFF(co_kwonlyargcount), READONLY},
 
211
    {"co_nlocals",      T_INT,          OFF(co_nlocals),        READONLY},
 
212
    {"co_stacksize",T_INT,              OFF(co_stacksize),      READONLY},
 
213
    {"co_flags",        T_INT,          OFF(co_flags),          READONLY},
 
214
    {"co_code",         T_OBJECT,       OFF(co_code),           READONLY},
 
215
    {"co_consts",       T_OBJECT,       OFF(co_consts),         READONLY},
 
216
    {"co_names",        T_OBJECT,       OFF(co_names),          READONLY},
 
217
    {"co_varnames",     T_OBJECT,       OFF(co_varnames),       READONLY},
 
218
    {"co_freevars",     T_OBJECT,       OFF(co_freevars),       READONLY},
 
219
    {"co_cellvars",     T_OBJECT,       OFF(co_cellvars),       READONLY},
 
220
    {"co_filename",     T_OBJECT,       OFF(co_filename),       READONLY},
 
221
    {"co_name",         T_OBJECT,       OFF(co_name),           READONLY},
 
222
    {"co_firstlineno", T_INT,           OFF(co_firstlineno),    READONLY},
 
223
    {"co_lnotab",       T_OBJECT,       OFF(co_lnotab),         READONLY},
 
224
    {NULL}      /* Sentinel */
 
225
};
 
226
 
 
227
/* Helper for code_new: return a shallow copy of a tuple that is
 
228
   guaranteed to contain exact strings, by converting string subclasses
 
229
   to exact strings and complaining if a non-string is found. */
 
230
static PyObject*
 
231
validate_and_copy_tuple(PyObject *tup)
 
232
{
 
233
    PyObject *newtuple;
 
234
    PyObject *item;
 
235
    Py_ssize_t i, len;
 
236
 
 
237
    len = PyTuple_GET_SIZE(tup);
 
238
    newtuple = PyTuple_New(len);
 
239
    if (newtuple == NULL)
 
240
        return NULL;
 
241
 
 
242
    for (i = 0; i < len; i++) {
 
243
        item = PyTuple_GET_ITEM(tup, i);
 
244
        if (PyUnicode_CheckExact(item)) {
 
245
            Py_INCREF(item);
 
246
        }
 
247
        else if (!PyUnicode_Check(item)) {
 
248
            PyErr_Format(
 
249
                PyExc_TypeError,
 
250
                "name tuples must contain only "
 
251
                "strings, not '%.500s'",
 
252
                item->ob_type->tp_name);
 
253
            Py_DECREF(newtuple);
 
254
            return NULL;
 
255
        }
 
256
        else {
 
257
            item = _PyUnicode_Copy(item);
 
258
            if (item == NULL) {
 
259
                Py_DECREF(newtuple);
 
260
                return NULL;
 
261
            }
 
262
        }
 
263
        PyTuple_SET_ITEM(newtuple, i, item);
 
264
    }
 
265
 
 
266
    return newtuple;
 
267
}
 
268
 
 
269
PyDoc_STRVAR(code_doc,
 
270
"code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
 
271
      constants, names, varnames, filename, name, firstlineno,\n\
 
272
      lnotab[, freevars[, cellvars]])\n\
 
273
\n\
 
274
Create a code object.  Not for the faint of heart.");
 
275
 
 
276
static PyObject *
 
277
code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
 
278
{
 
279
    int argcount;
 
280
    int kwonlyargcount;
 
281
    int nlocals;
 
282
    int stacksize;
 
283
    int flags;
 
284
    PyObject *co = NULL;
 
285
    PyObject *code;
 
286
    PyObject *consts;
 
287
    PyObject *names, *ournames = NULL;
 
288
    PyObject *varnames, *ourvarnames = NULL;
 
289
    PyObject *freevars = NULL, *ourfreevars = NULL;
 
290
    PyObject *cellvars = NULL, *ourcellvars = NULL;
 
291
    PyObject *filename;
 
292
    PyObject *name;
 
293
    int firstlineno;
 
294
    PyObject *lnotab;
 
295
 
 
296
    if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
 
297
                          &argcount, &kwonlyargcount,
 
298
                              &nlocals, &stacksize, &flags,
 
299
                          &code,
 
300
                          &PyTuple_Type, &consts,
 
301
                          &PyTuple_Type, &names,
 
302
                          &PyTuple_Type, &varnames,
 
303
                          &filename, &name,
 
304
                          &firstlineno, &lnotab,
 
305
                          &PyTuple_Type, &freevars,
 
306
                          &PyTuple_Type, &cellvars))
 
307
        return NULL;
 
308
 
 
309
    if (argcount < 0) {
 
310
        PyErr_SetString(
 
311
            PyExc_ValueError,
 
312
            "code: argcount must not be negative");
 
313
        goto cleanup;
 
314
    }
 
315
 
 
316
    if (kwonlyargcount < 0) {
 
317
        PyErr_SetString(
 
318
            PyExc_ValueError,
 
319
            "code: kwonlyargcount must not be negative");
 
320
        goto cleanup;
 
321
    }
 
322
    if (nlocals < 0) {
 
323
        PyErr_SetString(
 
324
            PyExc_ValueError,
 
325
            "code: nlocals must not be negative");
 
326
        goto cleanup;
 
327
    }
 
328
 
 
329
    ournames = validate_and_copy_tuple(names);
 
330
    if (ournames == NULL)
 
331
        goto cleanup;
 
332
    ourvarnames = validate_and_copy_tuple(varnames);
 
333
    if (ourvarnames == NULL)
 
334
        goto cleanup;
 
335
    if (freevars)
 
336
        ourfreevars = validate_and_copy_tuple(freevars);
 
337
    else
 
338
        ourfreevars = PyTuple_New(0);
 
339
    if (ourfreevars == NULL)
 
340
        goto cleanup;
 
341
    if (cellvars)
 
342
        ourcellvars = validate_and_copy_tuple(cellvars);
 
343
    else
 
344
        ourcellvars = PyTuple_New(0);
 
345
    if (ourcellvars == NULL)
 
346
        goto cleanup;
 
347
 
 
348
    co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
 
349
                                nlocals, stacksize, flags,
 
350
                                code, consts, ournames, ourvarnames,
 
351
                                ourfreevars, ourcellvars, filename,
 
352
                                name, firstlineno, lnotab);
 
353
  cleanup:
 
354
    Py_XDECREF(ournames);
 
355
    Py_XDECREF(ourvarnames);
 
356
    Py_XDECREF(ourfreevars);
 
357
    Py_XDECREF(ourcellvars);
 
358
    return co;
 
359
}
 
360
 
 
361
static void
 
362
code_dealloc(PyCodeObject *co)
 
363
{
 
364
    Py_XDECREF(co->co_code);
 
365
    Py_XDECREF(co->co_consts);
 
366
    Py_XDECREF(co->co_names);
 
367
    Py_XDECREF(co->co_varnames);
 
368
    Py_XDECREF(co->co_freevars);
 
369
    Py_XDECREF(co->co_cellvars);
 
370
    Py_XDECREF(co->co_filename);
 
371
    Py_XDECREF(co->co_name);
 
372
    Py_XDECREF(co->co_lnotab);
 
373
    if (co->co_cell2arg != NULL)
 
374
        PyMem_FREE(co->co_cell2arg);
 
375
    if (co->co_zombieframe != NULL)
 
376
        PyObject_GC_Del(co->co_zombieframe);
 
377
    if (co->co_weakreflist != NULL)
 
378
        PyObject_ClearWeakRefs((PyObject*)co);
 
379
    PyObject_DEL(co);
 
380
}
 
381
 
 
382
static PyObject *
 
383
code_sizeof(PyCodeObject *co, void *unused)
 
384
{
 
385
    Py_ssize_t res;
 
386
 
 
387
    res = sizeof(PyCodeObject);
 
388
    if (co->co_cell2arg != NULL && co->co_cellvars != NULL)
 
389
        res += PyTuple_GET_SIZE(co->co_cellvars) * sizeof(unsigned char);
 
390
    return PyLong_FromSsize_t(res);
 
391
}
 
392
 
 
393
static PyObject *
 
394
code_repr(PyCodeObject *co)
 
395
{
 
396
    int lineno;
 
397
    if (co->co_firstlineno != 0)
 
398
        lineno = co->co_firstlineno;
 
399
    else
 
400
        lineno = -1;
 
401
    if (co->co_filename && PyUnicode_Check(co->co_filename)) {
 
402
        return PyUnicode_FromFormat(
 
403
            "<code object %U at %p, file \"%U\", line %d>",
 
404
            co->co_name, co, co->co_filename, lineno);
 
405
    } else {
 
406
        return PyUnicode_FromFormat(
 
407
            "<code object %U at %p, file ???, line %d>",
 
408
            co->co_name, co, lineno);
 
409
    }
 
410
}
 
411
 
 
412
static PyObject *
 
413
code_richcompare(PyObject *self, PyObject *other, int op)
 
414
{
 
415
    PyCodeObject *co, *cp;
 
416
    int eq;
 
417
    PyObject *res;
 
418
 
 
419
    if ((op != Py_EQ && op != Py_NE) ||
 
420
        !PyCode_Check(self) ||
 
421
        !PyCode_Check(other)) {
 
422
        Py_RETURN_NOTIMPLEMENTED;
 
423
    }
 
424
 
 
425
    co = (PyCodeObject *)self;
 
426
    cp = (PyCodeObject *)other;
 
427
 
 
428
    eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
 
429
    if (eq <= 0) goto unequal;
 
430
    eq = co->co_argcount == cp->co_argcount;
 
431
    if (!eq) goto unequal;
 
432
    eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
 
433
    if (!eq) goto unequal;
 
434
    eq = co->co_nlocals == cp->co_nlocals;
 
435
    if (!eq) goto unequal;
 
436
    eq = co->co_flags == cp->co_flags;
 
437
    if (!eq) goto unequal;
 
438
    eq = co->co_firstlineno == cp->co_firstlineno;
 
439
    if (!eq) goto unequal;
 
440
    eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
 
441
    if (eq <= 0) goto unequal;
 
442
    eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
 
443
    if (eq <= 0) goto unequal;
 
444
    eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
 
445
    if (eq <= 0) goto unequal;
 
446
    eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
 
447
    if (eq <= 0) goto unequal;
 
448
    eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
 
449
    if (eq <= 0) goto unequal;
 
450
    eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
 
451
    if (eq <= 0) goto unequal;
 
452
 
 
453
    if (op == Py_EQ)
 
454
        res = Py_True;
 
455
    else
 
456
        res = Py_False;
 
457
    goto done;
 
458
 
 
459
  unequal:
 
460
    if (eq < 0)
 
461
        return NULL;
 
462
    if (op == Py_NE)
 
463
        res = Py_True;
 
464
    else
 
465
        res = Py_False;
 
466
 
 
467
  done:
 
468
    Py_INCREF(res);
 
469
    return res;
 
470
}
 
471
 
 
472
static Py_hash_t
 
473
code_hash(PyCodeObject *co)
 
474
{
 
475
    Py_hash_t h, h0, h1, h2, h3, h4, h5, h6;
 
476
    h0 = PyObject_Hash(co->co_name);
 
477
    if (h0 == -1) return -1;
 
478
    h1 = PyObject_Hash(co->co_code);
 
479
    if (h1 == -1) return -1;
 
480
    h2 = PyObject_Hash(co->co_consts);
 
481
    if (h2 == -1) return -1;
 
482
    h3 = PyObject_Hash(co->co_names);
 
483
    if (h3 == -1) return -1;
 
484
    h4 = PyObject_Hash(co->co_varnames);
 
485
    if (h4 == -1) return -1;
 
486
    h5 = PyObject_Hash(co->co_freevars);
 
487
    if (h5 == -1) return -1;
 
488
    h6 = PyObject_Hash(co->co_cellvars);
 
489
    if (h6 == -1) return -1;
 
490
    h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
 
491
        co->co_argcount ^ co->co_kwonlyargcount ^
 
492
        co->co_nlocals ^ co->co_flags;
 
493
    if (h == -1) h = -2;
 
494
    return h;
 
495
}
 
496
 
 
497
/* XXX code objects need to participate in GC? */
 
498
 
 
499
static struct PyMethodDef code_methods[] = {
 
500
    {"__sizeof__", (PyCFunction)code_sizeof, METH_NOARGS},
 
501
    {NULL, NULL}                /* sentinel */
 
502
};
 
503
 
 
504
PyTypeObject PyCode_Type = {
 
505
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
 
506
    "code",
 
507
    sizeof(PyCodeObject),
 
508
    0,
 
509
    (destructor)code_dealloc,           /* tp_dealloc */
 
510
    0,                                  /* tp_print */
 
511
    0,                                  /* tp_getattr */
 
512
    0,                                  /* tp_setattr */
 
513
    0,                                  /* tp_reserved */
 
514
    (reprfunc)code_repr,                /* tp_repr */
 
515
    0,                                  /* tp_as_number */
 
516
    0,                                  /* tp_as_sequence */
 
517
    0,                                  /* tp_as_mapping */
 
518
    (hashfunc)code_hash,                /* tp_hash */
 
519
    0,                                  /* tp_call */
 
520
    0,                                  /* tp_str */
 
521
    PyObject_GenericGetAttr,            /* tp_getattro */
 
522
    0,                                  /* tp_setattro */
 
523
    0,                                  /* tp_as_buffer */
 
524
    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
 
525
    code_doc,                           /* tp_doc */
 
526
    0,                                  /* tp_traverse */
 
527
    0,                                  /* tp_clear */
 
528
    code_richcompare,                   /* tp_richcompare */
 
529
    offsetof(PyCodeObject, co_weakreflist),     /* tp_weaklistoffset */
 
530
    0,                                  /* tp_iter */
 
531
    0,                                  /* tp_iternext */
 
532
    code_methods,                       /* tp_methods */
 
533
    code_memberlist,                    /* tp_members */
 
534
    0,                                  /* tp_getset */
 
535
    0,                                  /* tp_base */
 
536
    0,                                  /* tp_dict */
 
537
    0,                                  /* tp_descr_get */
 
538
    0,                                  /* tp_descr_set */
 
539
    0,                                  /* tp_dictoffset */
 
540
    0,                                  /* tp_init */
 
541
    0,                                  /* tp_alloc */
 
542
    code_new,                           /* tp_new */
 
543
};
 
544
 
 
545
/* Use co_lnotab to compute the line number from a bytecode index, addrq.  See
 
546
   lnotab_notes.txt for the details of the lnotab representation.
 
547
*/
 
548
 
 
549
int
 
550
PyCode_Addr2Line(PyCodeObject *co, int addrq)
 
551
{
 
552
    Py_ssize_t size = PyBytes_Size(co->co_lnotab) / 2;
 
553
    unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
 
554
    int line = co->co_firstlineno;
 
555
    int addr = 0;
 
556
    while (--size >= 0) {
 
557
        addr += *p++;
 
558
        if (addr > addrq)
 
559
            break;
 
560
        line += *p++;
 
561
    }
 
562
    return line;
 
563
}
 
564
 
 
565
/* Update *bounds to describe the first and one-past-the-last instructions in
 
566
   the same line as lasti.  Return the number of that line. */
 
567
int
 
568
_PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
 
569
{
 
570
    Py_ssize_t size;
 
571
    int addr, line;
 
572
    unsigned char* p;
 
573
 
 
574
    p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
 
575
    size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
 
576
 
 
577
    addr = 0;
 
578
    line = co->co_firstlineno;
 
579
    assert(line > 0);
 
580
 
 
581
    /* possible optimization: if f->f_lasti == instr_ub
 
582
       (likely to be a common case) then we already know
 
583
       instr_lb -- if we stored the matching value of p
 
584
       somwhere we could skip the first while loop. */
 
585
 
 
586
    /* See lnotab_notes.txt for the description of
 
587
       co_lnotab.  A point to remember: increments to p
 
588
       come in (addr, line) pairs. */
 
589
 
 
590
    bounds->ap_lower = 0;
 
591
    while (size > 0) {
 
592
        if (addr + *p > lasti)
 
593
            break;
 
594
        addr += *p++;
 
595
        if (*p)
 
596
            bounds->ap_lower = addr;
 
597
        line += *p++;
 
598
        --size;
 
599
    }
 
600
 
 
601
    if (size > 0) {
 
602
        while (--size >= 0) {
 
603
            addr += *p++;
 
604
            if (*p++)
 
605
                break;
 
606
        }
 
607
        bounds->ap_upper = addr;
 
608
    }
 
609
    else {
 
610
        bounds->ap_upper = INT_MAX;
 
611
    }
 
612
 
 
613
    return line;
 
614
}