• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
all_name_chars(PyObject * o)11 all_name_chars(PyObject *o)
12 {
13     static char ok_name_char[256];
14     static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
15     const unsigned char *s, *e;
16 
17     if (ok_name_char[*name_chars] == 0) {
18         const unsigned char *p;
19         for (p = name_chars; *p; p++)
20             ok_name_char[*p] = 1;
21     }
22     s = (unsigned char *)PyString_AS_STRING(o);
23     e = s + PyString_GET_SIZE(o);
24     while (s != e) {
25         if (ok_name_char[*s++] == 0)
26             return 0;
27     }
28     return 1;
29 }
30 
31 static void
intern_strings(PyObject * tuple)32 intern_strings(PyObject *tuple)
33 {
34     Py_ssize_t i;
35 
36     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
37         PyObject *v = PyTuple_GET_ITEM(tuple, i);
38         if (v == NULL || !PyString_CheckExact(v)) {
39             Py_FatalError("non-string found in code slot");
40         }
41         PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
42     }
43 }
44 
45 /* Intern selected string constants */
46 static int
intern_string_constants(PyObject * tuple)47 intern_string_constants(PyObject *tuple)
48 {
49     int modified = 0;
50     Py_ssize_t i;
51 
52     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
53         PyObject *v = PyTuple_GET_ITEM(tuple, i);
54         if (PyString_CheckExact(v)) {
55             if (all_name_chars(v)) {
56                 PyObject *w = v;
57                 PyString_InternInPlace(&v);
58                 if (w != v) {
59                     PyTuple_SET_ITEM(tuple, i, v);
60                     modified = 1;
61                 }
62             }
63         }
64         else if (PyTuple_CheckExact(v)) {
65             intern_string_constants(v);
66         }
67         else if (PyFrozenSet_CheckExact(v)) {
68             PyObject *w = v;
69             PyObject *tmp = PySequence_Tuple(v);
70             if (tmp == NULL) {
71                 PyErr_Clear();
72                 continue;
73             }
74             if (intern_string_constants(tmp)) {
75                 v = PyFrozenSet_New(tmp);
76                 if (v == NULL) {
77                     PyErr_Clear();
78                 }
79                 else {
80                     PyTuple_SET_ITEM(tuple, i, v);
81                     Py_DECREF(w);
82                     modified = 1;
83                 }
84             }
85             Py_DECREF(tmp);
86         }
87     }
88     return modified;
89 }
90 
91 
92 PyCodeObject *
PyCode_New(int argcount,int nlocals,int stacksize,int flags,PyObject * code,PyObject * consts,PyObject * names,PyObject * varnames,PyObject * freevars,PyObject * cellvars,PyObject * filename,PyObject * name,int firstlineno,PyObject * lnotab)93 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
94            PyObject *code, PyObject *consts, PyObject *names,
95            PyObject *varnames, PyObject *freevars, PyObject *cellvars,
96            PyObject *filename, PyObject *name, int firstlineno,
97            PyObject *lnotab)
98 {
99     PyCodeObject *co;
100     /* Check argument types */
101     if (argcount < 0 || nlocals < 0 ||
102         code == NULL ||
103         consts == NULL || !PyTuple_Check(consts) ||
104         names == NULL || !PyTuple_Check(names) ||
105         varnames == NULL || !PyTuple_Check(varnames) ||
106         freevars == NULL || !PyTuple_Check(freevars) ||
107         cellvars == NULL || !PyTuple_Check(cellvars) ||
108         name == NULL || !PyString_Check(name) ||
109         filename == NULL || !PyString_Check(filename) ||
110         lnotab == NULL || !PyString_Check(lnotab) ||
111         !PyObject_CheckReadBuffer(code)) {
112         PyErr_BadInternalCall();
113         return NULL;
114     }
115     intern_strings(names);
116     intern_strings(varnames);
117     intern_strings(freevars);
118     intern_strings(cellvars);
119     intern_string_constants(consts);
120     co = PyObject_NEW(PyCodeObject, &PyCode_Type);
121     if (co != NULL) {
122         co->co_argcount = argcount;
123         co->co_nlocals = nlocals;
124         co->co_stacksize = stacksize;
125         co->co_flags = flags;
126         Py_INCREF(code);
127         co->co_code = code;
128         Py_INCREF(consts);
129         co->co_consts = consts;
130         Py_INCREF(names);
131         co->co_names = names;
132         Py_INCREF(varnames);
133         co->co_varnames = varnames;
134         Py_INCREF(freevars);
135         co->co_freevars = freevars;
136         Py_INCREF(cellvars);
137         co->co_cellvars = cellvars;
138         Py_INCREF(filename);
139         co->co_filename = filename;
140         Py_INCREF(name);
141         co->co_name = name;
142         co->co_firstlineno = firstlineno;
143         Py_INCREF(lnotab);
144         co->co_lnotab = lnotab;
145         co->co_zombieframe = NULL;
146         co->co_weakreflist = NULL;
147     }
148     return co;
149 }
150 
151 PyCodeObject *
PyCode_NewEmpty(const char * filename,const char * funcname,int firstlineno)152 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
153 {
154     static PyObject *emptystring = NULL;
155     static PyObject *nulltuple = NULL;
156     PyObject *filename_ob = NULL;
157     PyObject *funcname_ob = NULL;
158     PyCodeObject *result = NULL;
159     if (emptystring == NULL) {
160         emptystring = PyString_FromString("");
161         if (emptystring == NULL)
162             goto failed;
163     }
164     if (nulltuple == NULL) {
165         nulltuple = PyTuple_New(0);
166         if (nulltuple == NULL)
167             goto failed;
168     }
169     funcname_ob = PyString_FromString(funcname);
170     if (funcname_ob == NULL)
171         goto failed;
172     filename_ob = PyString_FromString(filename);
173     if (filename_ob == NULL)
174         goto failed;
175 
176     result = PyCode_New(0,                      /* argcount */
177                 0,                              /* nlocals */
178                 0,                              /* stacksize */
179                 0,                              /* flags */
180                 emptystring,                    /* code */
181                 nulltuple,                      /* consts */
182                 nulltuple,                      /* names */
183                 nulltuple,                      /* varnames */
184                 nulltuple,                      /* freevars */
185                 nulltuple,                      /* cellvars */
186                 filename_ob,                    /* filename */
187                 funcname_ob,                    /* name */
188                 firstlineno,                    /* firstlineno */
189                 emptystring                     /* lnotab */
190                 );
191 
192 failed:
193     Py_XDECREF(funcname_ob);
194     Py_XDECREF(filename_ob);
195     return result;
196 }
197 
198 #define OFF(x) offsetof(PyCodeObject, x)
199 
200 static PyMemberDef code_memberlist[] = {
201     {"co_argcount",     T_INT,          OFF(co_argcount),       READONLY},
202     {"co_nlocals",      T_INT,          OFF(co_nlocals),        READONLY},
203     {"co_stacksize",T_INT,              OFF(co_stacksize),      READONLY},
204     {"co_flags",        T_INT,          OFF(co_flags),          READONLY},
205     {"co_code",         T_OBJECT,       OFF(co_code),           READONLY},
206     {"co_consts",       T_OBJECT,       OFF(co_consts),         READONLY},
207     {"co_names",        T_OBJECT,       OFF(co_names),          READONLY},
208     {"co_varnames",     T_OBJECT,       OFF(co_varnames),       READONLY},
209     {"co_freevars",     T_OBJECT,       OFF(co_freevars),       READONLY},
210     {"co_cellvars",     T_OBJECT,       OFF(co_cellvars),       READONLY},
211     {"co_filename",     T_OBJECT,       OFF(co_filename),       READONLY},
212     {"co_name",         T_OBJECT,       OFF(co_name),           READONLY},
213     {"co_firstlineno", T_INT,           OFF(co_firstlineno),    READONLY},
214     {"co_lnotab",       T_OBJECT,       OFF(co_lnotab),         READONLY},
215     {NULL}      /* Sentinel */
216 };
217 
218 /* Helper for code_new: return a shallow copy of a tuple that is
219    guaranteed to contain exact strings, by converting string subclasses
220    to exact strings and complaining if a non-string is found. */
221 static PyObject*
validate_and_copy_tuple(PyObject * tup)222 validate_and_copy_tuple(PyObject *tup)
223 {
224     PyObject *newtuple;
225     PyObject *item;
226     Py_ssize_t i, len;
227 
228     len = PyTuple_GET_SIZE(tup);
229     newtuple = PyTuple_New(len);
230     if (newtuple == NULL)
231         return NULL;
232 
233     for (i = 0; i < len; i++) {
234         item = PyTuple_GET_ITEM(tup, i);
235         if (PyString_CheckExact(item)) {
236             Py_INCREF(item);
237         }
238         else if (!PyString_Check(item)) {
239             PyErr_Format(
240                 PyExc_TypeError,
241                 "name tuples must contain only "
242                 "strings, not '%.500s'",
243                 item->ob_type->tp_name);
244             Py_DECREF(newtuple);
245             return NULL;
246         }
247         else {
248             item = PyString_FromStringAndSize(
249                 PyString_AS_STRING(item),
250                 PyString_GET_SIZE(item));
251             if (item == NULL) {
252                 Py_DECREF(newtuple);
253                 return NULL;
254             }
255         }
256         PyTuple_SET_ITEM(newtuple, i, item);
257     }
258 
259     return newtuple;
260 }
261 
262 PyDoc_STRVAR(code_doc,
263 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
264       varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
265 \n\
266 Create a code object.  Not for the faint of heart.");
267 
268 static PyObject *
code_new(PyTypeObject * type,PyObject * args,PyObject * kw)269 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
270 {
271     int argcount;
272     int nlocals;
273     int stacksize;
274     int flags;
275     PyObject *co = NULL;
276     PyObject *code;
277     PyObject *consts;
278     PyObject *names, *ournames = NULL;
279     PyObject *varnames, *ourvarnames = NULL;
280     PyObject *freevars = NULL, *ourfreevars = NULL;
281     PyObject *cellvars = NULL, *ourcellvars = NULL;
282     PyObject *filename;
283     PyObject *name;
284     int firstlineno;
285     PyObject *lnotab;
286 
287     if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
288                           &argcount, &nlocals, &stacksize, &flags,
289                           &code,
290                           &PyTuple_Type, &consts,
291                           &PyTuple_Type, &names,
292                           &PyTuple_Type, &varnames,
293                           &filename, &name,
294                           &firstlineno, &lnotab,
295                           &PyTuple_Type, &freevars,
296                           &PyTuple_Type, &cellvars))
297         return NULL;
298 
299     if (argcount < 0) {
300         PyErr_SetString(
301             PyExc_ValueError,
302             "code: argcount must not be negative");
303         goto cleanup;
304     }
305 
306     if (nlocals < 0) {
307         PyErr_SetString(
308             PyExc_ValueError,
309             "code: nlocals must not be negative");
310         goto cleanup;
311     }
312 
313     ournames = validate_and_copy_tuple(names);
314     if (ournames == NULL)
315         goto cleanup;
316     ourvarnames = validate_and_copy_tuple(varnames);
317     if (ourvarnames == NULL)
318         goto cleanup;
319     if (freevars)
320         ourfreevars = validate_and_copy_tuple(freevars);
321     else
322         ourfreevars = PyTuple_New(0);
323     if (ourfreevars == NULL)
324         goto cleanup;
325     if (cellvars)
326         ourcellvars = validate_and_copy_tuple(cellvars);
327     else
328         ourcellvars = PyTuple_New(0);
329     if (ourcellvars == NULL)
330         goto cleanup;
331 
332     co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
333                                 code, consts, ournames, ourvarnames,
334                                 ourfreevars, ourcellvars, filename,
335                                 name, firstlineno, lnotab);
336   cleanup:
337     Py_XDECREF(ournames);
338     Py_XDECREF(ourvarnames);
339     Py_XDECREF(ourfreevars);
340     Py_XDECREF(ourcellvars);
341     return co;
342 }
343 
344 static void
code_dealloc(PyCodeObject * co)345 code_dealloc(PyCodeObject *co)
346 {
347     Py_XDECREF(co->co_code);
348     Py_XDECREF(co->co_consts);
349     Py_XDECREF(co->co_names);
350     Py_XDECREF(co->co_varnames);
351     Py_XDECREF(co->co_freevars);
352     Py_XDECREF(co->co_cellvars);
353     Py_XDECREF(co->co_filename);
354     Py_XDECREF(co->co_name);
355     Py_XDECREF(co->co_lnotab);
356     if (co->co_zombieframe != NULL)
357         PyObject_GC_Del(co->co_zombieframe);
358     if (co->co_weakreflist != NULL)
359         PyObject_ClearWeakRefs((PyObject*)co);
360     PyObject_DEL(co);
361 }
362 
363 static PyObject *
code_repr(PyCodeObject * co)364 code_repr(PyCodeObject *co)
365 {
366     char buf[500];
367     int lineno = -1;
368     char *filename = "???";
369     char *name = "???";
370 
371     if (co->co_firstlineno != 0)
372         lineno = co->co_firstlineno;
373     if (co->co_filename && PyString_Check(co->co_filename))
374         filename = PyString_AS_STRING(co->co_filename);
375     if (co->co_name && PyString_Check(co->co_name))
376         name = PyString_AS_STRING(co->co_name);
377     PyOS_snprintf(buf, sizeof(buf),
378                   "<code object %.100s at %p, file \"%.300s\", line %d>",
379                   name, co, filename, lineno);
380     return PyString_FromString(buf);
381 }
382 
383 static int
code_compare(PyCodeObject * co,PyCodeObject * cp)384 code_compare(PyCodeObject *co, PyCodeObject *cp)
385 {
386     int cmp;
387     cmp = PyObject_Compare(co->co_name, cp->co_name);
388     if (cmp) return cmp;
389     cmp = co->co_argcount - cp->co_argcount;
390     if (cmp) goto normalize;
391     cmp = co->co_nlocals - cp->co_nlocals;
392     if (cmp) goto normalize;
393     cmp = co->co_flags - cp->co_flags;
394     if (cmp) goto normalize;
395     cmp = co->co_firstlineno - cp->co_firstlineno;
396     if (cmp) goto normalize;
397     cmp = PyObject_Compare(co->co_code, cp->co_code);
398     if (cmp) return cmp;
399     cmp = PyObject_Compare(co->co_consts, cp->co_consts);
400     if (cmp) return cmp;
401     cmp = PyObject_Compare(co->co_names, cp->co_names);
402     if (cmp) return cmp;
403     cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
404     if (cmp) return cmp;
405     cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
406     if (cmp) return cmp;
407     cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
408     return cmp;
409 
410  normalize:
411     if (cmp > 0)
412         return 1;
413     else if (cmp < 0)
414         return -1;
415     else
416         return 0;
417 }
418 
419 PyObject*
_PyCode_ConstantKey(PyObject * op)420 _PyCode_ConstantKey(PyObject *op)
421 {
422     PyObject *key;
423 
424     /* Py_None is a singleton */
425     if (op == Py_None
426        || PyInt_CheckExact(op)
427        || PyLong_CheckExact(op)
428        || PyBool_Check(op)
429        || PyBytes_CheckExact(op)
430 #ifdef Py_USING_UNICODE
431        || PyUnicode_CheckExact(op)
432 #endif
433           /* code_richcompare() uses _PyCode_ConstantKey() internally */
434        || PyCode_Check(op)) {
435         key = PyTuple_Pack(2, Py_TYPE(op), op);
436     }
437     else if (PyFloat_CheckExact(op)) {
438         double d = PyFloat_AS_DOUBLE(op);
439         /* all we need is to make the tuple different in either the 0.0
440          * or -0.0 case from all others, just to avoid the "coercion".
441          */
442         if (d == 0.0 && copysign(1.0, d) < 0.0)
443             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
444         else
445             key = PyTuple_Pack(2, Py_TYPE(op), op);
446     }
447 #ifndef WITHOUT_COMPLEX
448     else if (PyComplex_CheckExact(op)) {
449         Py_complex z;
450         int real_negzero, imag_negzero;
451         /* For the complex case we must make complex(x, 0.)
452            different from complex(x, -0.) and complex(0., y)
453            different from complex(-0., y), for any x and y.
454            All four complex zeros must be distinguished.*/
455         z = PyComplex_AsCComplex(op);
456         real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
457         imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
458         /* use True, False and None singleton as tags for the real and imag
459          * sign, to make tuples different */
460         if (real_negzero && imag_negzero) {
461             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True);
462         }
463         else if (imag_negzero) {
464             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False);
465         }
466         else if (real_negzero) {
467             key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None);
468         }
469         else {
470             key = PyTuple_Pack(2, Py_TYPE(op), op);
471         }
472     }
473 #endif
474     else if (PyTuple_CheckExact(op)) {
475         Py_ssize_t i, len;
476         PyObject *tuple;
477 
478         len = PyTuple_GET_SIZE(op);
479         tuple = PyTuple_New(len);
480         if (tuple == NULL)
481             return NULL;
482 
483         for (i=0; i < len; i++) {
484             PyObject *item, *item_key;
485 
486             item = PyTuple_GET_ITEM(op, i);
487             item_key = _PyCode_ConstantKey(item);
488             if (item_key == NULL) {
489                 Py_DECREF(tuple);
490                 return NULL;
491             }
492 
493             PyTuple_SET_ITEM(tuple, i, item_key);
494         }
495 
496         key = PyTuple_Pack(3, Py_TYPE(op), op, tuple);
497         Py_DECREF(tuple);
498     }
499     else if (PyFrozenSet_CheckExact(op)) {
500         Py_ssize_t pos = 0;
501         PyObject *item;
502         long hash;
503         Py_ssize_t i, len;
504         PyObject *tuple, *set;
505 
506         len = PySet_GET_SIZE(op);
507         tuple = PyTuple_New(len);
508         if (tuple == NULL)
509             return NULL;
510 
511         i = 0;
512         while (_PySet_NextEntry(op, &pos, &item, &hash)) {
513             PyObject *item_key;
514 
515             item_key = _PyCode_ConstantKey(item);
516             if (item_key == NULL) {
517                 Py_DECREF(tuple);
518                 return NULL;
519             }
520 
521             assert(i < len);
522             PyTuple_SET_ITEM(tuple, i, item_key);
523             i++;
524         }
525         set = PyFrozenSet_New(tuple);
526         Py_DECREF(tuple);
527         if (set == NULL)
528             return NULL;
529 
530         key = PyTuple_Pack(3, Py_TYPE(op), op, set);
531         Py_DECREF(set);
532         return key;
533     }
534     else {
535         /* for other types, use the object identifier as a unique identifier
536          * to ensure that they are seen as unequal. */
537         PyObject *obj_id = PyLong_FromVoidPtr(op);
538         if (obj_id == NULL)
539             return NULL;
540 
541         key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id);
542         Py_DECREF(obj_id);
543     }
544     return key;
545 }
546 
547 static PyObject *
code_richcompare(PyObject * self,PyObject * other,int op)548 code_richcompare(PyObject *self, PyObject *other, int op)
549 {
550     PyCodeObject *co, *cp;
551     int eq;
552     PyObject *consts1, *consts2;
553     PyObject *res;
554 
555     if ((op != Py_EQ && op != Py_NE) ||
556         !PyCode_Check(self) ||
557         !PyCode_Check(other)) {
558 
559         /* Py3K warning if types are not equal and comparison
560         isn't == or !=  */
561         if (PyErr_WarnPy3k("code inequality comparisons not supported "
562                            "in 3.x", 1) < 0) {
563             return NULL;
564         }
565 
566         Py_INCREF(Py_NotImplemented);
567         return Py_NotImplemented;
568     }
569 
570     co = (PyCodeObject *)self;
571     cp = (PyCodeObject *)other;
572 
573     eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
574     if (eq <= 0) goto unequal;
575     eq = co->co_argcount == cp->co_argcount;
576     if (!eq) goto unequal;
577     eq = co->co_nlocals == cp->co_nlocals;
578     if (!eq) goto unequal;
579     eq = co->co_flags == cp->co_flags;
580     if (!eq) goto unequal;
581     eq = co->co_firstlineno == cp->co_firstlineno;
582     if (!eq) goto unequal;
583     eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
584     if (eq <= 0) goto unequal;
585 
586     /* compare constants */
587     consts1 = _PyCode_ConstantKey(co->co_consts);
588     if (!consts1)
589         return NULL;
590     consts2 = _PyCode_ConstantKey(cp->co_consts);
591     if (!consts2) {
592         Py_DECREF(consts1);
593         return NULL;
594     }
595     eq = PyObject_RichCompareBool(consts1, consts2, Py_EQ);
596     Py_DECREF(consts1);
597     Py_DECREF(consts2);
598     if (eq <= 0) goto unequal;
599 
600     eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
601     if (eq <= 0) goto unequal;
602     eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
603     if (eq <= 0) goto unequal;
604     eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
605     if (eq <= 0) goto unequal;
606     eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
607     if (eq <= 0) goto unequal;
608 
609     if (op == Py_EQ)
610         res = Py_True;
611     else
612         res = Py_False;
613     goto done;
614 
615   unequal:
616     if (eq < 0)
617         return NULL;
618     if (op == Py_NE)
619         res = Py_True;
620     else
621         res = Py_False;
622 
623   done:
624     Py_INCREF(res);
625     return res;
626 }
627 
628 static long
code_hash(PyCodeObject * co)629 code_hash(PyCodeObject *co)
630 {
631     long h, h0, h1, h2, h3, h4, h5, h6;
632     h0 = PyObject_Hash(co->co_name);
633     if (h0 == -1) return -1;
634     h1 = PyObject_Hash(co->co_code);
635     if (h1 == -1) return -1;
636     h2 = PyObject_Hash(co->co_consts);
637     if (h2 == -1) return -1;
638     h3 = PyObject_Hash(co->co_names);
639     if (h3 == -1) return -1;
640     h4 = PyObject_Hash(co->co_varnames);
641     if (h4 == -1) return -1;
642     h5 = PyObject_Hash(co->co_freevars);
643     if (h5 == -1) return -1;
644     h6 = PyObject_Hash(co->co_cellvars);
645     if (h6 == -1) return -1;
646     h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
647         co->co_argcount ^ co->co_nlocals ^ co->co_flags;
648     if (h == -1) h = -2;
649     return h;
650 }
651 
652 /* XXX code objects need to participate in GC? */
653 
654 PyTypeObject PyCode_Type = {
655     PyVarObject_HEAD_INIT(&PyType_Type, 0)
656     "code",
657     sizeof(PyCodeObject),
658     0,
659     (destructor)code_dealloc,           /* tp_dealloc */
660     0,                                  /* tp_print */
661     0,                                  /* tp_getattr */
662     0,                                  /* tp_setattr */
663     (cmpfunc)code_compare,              /* tp_compare */
664     (reprfunc)code_repr,                /* tp_repr */
665     0,                                  /* tp_as_number */
666     0,                                  /* tp_as_sequence */
667     0,                                  /* tp_as_mapping */
668     (hashfunc)code_hash,                /* tp_hash */
669     0,                                  /* tp_call */
670     0,                                  /* tp_str */
671     PyObject_GenericGetAttr,            /* tp_getattro */
672     0,                                  /* tp_setattro */
673     0,                                  /* tp_as_buffer */
674     Py_TPFLAGS_DEFAULT,                 /* tp_flags */
675     code_doc,                           /* tp_doc */
676     0,                                  /* tp_traverse */
677     0,                                  /* tp_clear */
678     code_richcompare,                   /* tp_richcompare */
679     offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
680     0,                                  /* tp_iter */
681     0,                                  /* tp_iternext */
682     0,                                  /* tp_methods */
683     code_memberlist,                    /* tp_members */
684     0,                                  /* tp_getset */
685     0,                                  /* tp_base */
686     0,                                  /* tp_dict */
687     0,                                  /* tp_descr_get */
688     0,                                  /* tp_descr_set */
689     0,                                  /* tp_dictoffset */
690     0,                                  /* tp_init */
691     0,                                  /* tp_alloc */
692     code_new,                           /* tp_new */
693 };
694 
695 /* Use co_lnotab to compute the line number from a bytecode index, addrq.  See
696    lnotab_notes.txt for the details of the lnotab representation.
697 */
698 
699 int
PyCode_Addr2Line(PyCodeObject * co,int addrq)700 PyCode_Addr2Line(PyCodeObject *co, int addrq)
701 {
702     int size = PyString_Size(co->co_lnotab) / 2;
703     unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
704     int line = co->co_firstlineno;
705     int addr = 0;
706     while (--size >= 0) {
707         addr += *p++;
708         if (addr > addrq)
709             break;
710         line += *p++;
711     }
712     return line;
713 }
714 
715 /* Update *bounds to describe the first and one-past-the-last instructions in
716    the same line as lasti.  Return the number of that line. */
717 int
_PyCode_CheckLineNumber(PyCodeObject * co,int lasti,PyAddrPair * bounds)718 _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
719 {
720     int size, addr, line;
721     unsigned char* p;
722 
723     p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
724     size = PyString_GET_SIZE(co->co_lnotab) / 2;
725 
726     addr = 0;
727     line = co->co_firstlineno;
728     assert(line > 0);
729 
730     /* possible optimization: if f->f_lasti == instr_ub
731        (likely to be a common case) then we already know
732        instr_lb -- if we stored the matching value of p
733        somewhere we could skip the first while loop. */
734 
735     /* See lnotab_notes.txt for the description of
736        co_lnotab.  A point to remember: increments to p
737        come in (addr, line) pairs. */
738 
739     bounds->ap_lower = 0;
740     while (size > 0) {
741         if (addr + *p > lasti)
742             break;
743         addr += *p++;
744         if (*p)
745             bounds->ap_lower = addr;
746         line += *p++;
747         --size;
748     }
749 
750     if (size > 0) {
751         while (--size >= 0) {
752             addr += *p++;
753             if (*p++)
754                 break;
755         }
756         bounds->ap_upper = addr;
757     }
758     else {
759         bounds->ap_upper = INT_MAX;
760     }
761 
762     return line;
763 }
764