3 #include "structmember.h"
6 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
8 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
11 all_name_chars(unsigned char *s
)
13 static char ok_name_char
[256];
14 static unsigned char *name_chars
= (unsigned char *)NAME_CHARS
;
16 if (ok_name_char
[*name_chars
] == 0) {
18 for (p
= name_chars
; *p
; p
++)
22 if (ok_name_char
[*s
++] == 0)
29 intern_strings(PyObject
*tuple
)
33 for (i
= PyTuple_GET_SIZE(tuple
); --i
>= 0; ) {
34 PyObject
*v
= PyTuple_GET_ITEM(tuple
, i
);
35 if (v
== NULL
|| !PyString_CheckExact(v
)) {
36 Py_FatalError("non-string found in code slot");
38 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple
, i
));
44 PyCode_New(int argcount
, int nlocals
, int stacksize
, int flags
,
45 PyObject
*code
, PyObject
*consts
, PyObject
*names
,
46 PyObject
*varnames
, PyObject
*freevars
, PyObject
*cellvars
,
47 PyObject
*filename
, PyObject
*name
, int firstlineno
,
52 /* Check argument types */
53 if (argcount
< 0 || nlocals
< 0 ||
55 consts
== NULL
|| !PyTuple_Check(consts
) ||
56 names
== NULL
|| !PyTuple_Check(names
) ||
57 varnames
== NULL
|| !PyTuple_Check(varnames
) ||
58 freevars
== NULL
|| !PyTuple_Check(freevars
) ||
59 cellvars
== NULL
|| !PyTuple_Check(cellvars
) ||
60 name
== NULL
|| !PyString_Check(name
) ||
61 filename
== NULL
|| !PyString_Check(filename
) ||
62 lnotab
== NULL
|| !PyString_Check(lnotab
) ||
63 !PyObject_CheckReadBuffer(code
)) {
64 PyErr_BadInternalCall();
67 intern_strings(names
);
68 intern_strings(varnames
);
69 intern_strings(freevars
);
70 intern_strings(cellvars
);
71 /* Intern selected string constants */
72 for (i
= PyTuple_Size(consts
); --i
>= 0; ) {
73 PyObject
*v
= PyTuple_GetItem(consts
, i
);
74 if (!PyString_Check(v
))
76 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v
)))
78 PyString_InternInPlace(&PyTuple_GET_ITEM(consts
, i
));
80 co
= PyObject_NEW(PyCodeObject
, &PyCode_Type
);
82 co
->co_argcount
= argcount
;
83 co
->co_nlocals
= nlocals
;
84 co
->co_stacksize
= stacksize
;
89 co
->co_consts
= consts
;
93 co
->co_varnames
= varnames
;
95 co
->co_freevars
= freevars
;
97 co
->co_cellvars
= cellvars
;
99 co
->co_filename
= filename
;
102 co
->co_firstlineno
= firstlineno
;
104 co
->co_lnotab
= lnotab
;
105 co
->co_zombieframe
= NULL
;
111 #define OFF(x) offsetof(PyCodeObject, x)
113 static PyMemberDef code_memberlist
[] = {
114 {"co_argcount", T_INT
, OFF(co_argcount
), READONLY
},
115 {"co_nlocals", T_INT
, OFF(co_nlocals
), READONLY
},
116 {"co_stacksize",T_INT
, OFF(co_stacksize
), READONLY
},
117 {"co_flags", T_INT
, OFF(co_flags
), READONLY
},
118 {"co_code", T_OBJECT
, OFF(co_code
), READONLY
},
119 {"co_consts", T_OBJECT
, OFF(co_consts
), READONLY
},
120 {"co_names", T_OBJECT
, OFF(co_names
), READONLY
},
121 {"co_varnames", T_OBJECT
, OFF(co_varnames
), READONLY
},
122 {"co_freevars", T_OBJECT
, OFF(co_freevars
), READONLY
},
123 {"co_cellvars", T_OBJECT
, OFF(co_cellvars
), READONLY
},
124 {"co_filename", T_OBJECT
, OFF(co_filename
), READONLY
},
125 {"co_name", T_OBJECT
, OFF(co_name
), READONLY
},
126 {"co_firstlineno", T_INT
, OFF(co_firstlineno
), READONLY
},
127 {"co_lnotab", T_OBJECT
, OFF(co_lnotab
), READONLY
},
128 {NULL
} /* Sentinel */
131 /* Helper for code_new: return a shallow copy of a tuple that is
132 guaranteed to contain exact strings, by converting string subclasses
133 to exact strings and complaining if a non-string is found. */
135 validate_and_copy_tuple(PyObject
*tup
)
141 len
= PyTuple_GET_SIZE(tup
);
142 newtuple
= PyTuple_New(len
);
143 if (newtuple
== NULL
)
146 for (i
= 0; i
< len
; i
++) {
147 item
= PyTuple_GET_ITEM(tup
, i
);
148 if (PyString_CheckExact(item
)) {
151 else if (!PyString_Check(item
)) {
154 "name tuples must contain only "
155 "strings, not '%.500s'",
156 item
->ob_type
->tp_name
);
161 item
= PyString_FromStringAndSize(
162 PyString_AS_STRING(item
),
163 PyString_GET_SIZE(item
));
169 PyTuple_SET_ITEM(newtuple
, i
, item
);
175 PyDoc_STRVAR(code_doc
,
176 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
177 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
179 Create a code object. Not for the faint of heart.");
182 code_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kw
)
191 PyObject
*names
, *ournames
= NULL
;
192 PyObject
*varnames
, *ourvarnames
= NULL
;
193 PyObject
*freevars
= NULL
, *ourfreevars
= NULL
;
194 PyObject
*cellvars
= NULL
, *ourcellvars
= NULL
;
200 if (!PyArg_ParseTuple(args
, "iiiiSO!O!O!SSiS|O!O!:code",
201 &argcount
, &nlocals
, &stacksize
, &flags
,
203 &PyTuple_Type
, &consts
,
204 &PyTuple_Type
, &names
,
205 &PyTuple_Type
, &varnames
,
207 &firstlineno
, &lnotab
,
208 &PyTuple_Type
, &freevars
,
209 &PyTuple_Type
, &cellvars
))
215 "code: argcount must not be negative");
222 "code: nlocals must not be negative");
226 ournames
= validate_and_copy_tuple(names
);
227 if (ournames
== NULL
)
229 ourvarnames
= validate_and_copy_tuple(varnames
);
230 if (ourvarnames
== NULL
)
233 ourfreevars
= validate_and_copy_tuple(freevars
);
235 ourfreevars
= PyTuple_New(0);
236 if (ourfreevars
== NULL
)
239 ourcellvars
= validate_and_copy_tuple(cellvars
);
241 ourcellvars
= PyTuple_New(0);
242 if (ourcellvars
== NULL
)
245 co
= (PyObject
*)PyCode_New(argcount
, nlocals
, stacksize
, flags
,
246 code
, consts
, ournames
, ourvarnames
,
247 ourfreevars
, ourcellvars
, filename
,
248 name
, firstlineno
, lnotab
);
250 Py_XDECREF(ournames
);
251 Py_XDECREF(ourvarnames
);
252 Py_XDECREF(ourfreevars
);
253 Py_XDECREF(ourcellvars
);
258 code_dealloc(PyCodeObject
*co
)
260 Py_XDECREF(co
->co_code
);
261 Py_XDECREF(co
->co_consts
);
262 Py_XDECREF(co
->co_names
);
263 Py_XDECREF(co
->co_varnames
);
264 Py_XDECREF(co
->co_freevars
);
265 Py_XDECREF(co
->co_cellvars
);
266 Py_XDECREF(co
->co_filename
);
267 Py_XDECREF(co
->co_name
);
268 Py_XDECREF(co
->co_lnotab
);
269 if (co
->co_zombieframe
!= NULL
)
270 PyObject_GC_Del(co
->co_zombieframe
);
275 code_repr(PyCodeObject
*co
)
279 char *filename
= "???";
282 if (co
->co_firstlineno
!= 0)
283 lineno
= co
->co_firstlineno
;
284 if (co
->co_filename
&& PyString_Check(co
->co_filename
))
285 filename
= PyString_AS_STRING(co
->co_filename
);
286 if (co
->co_name
&& PyString_Check(co
->co_name
))
287 name
= PyString_AS_STRING(co
->co_name
);
288 PyOS_snprintf(buf
, sizeof(buf
),
289 "<code object %.100s at %p, file \"%.300s\", line %d>",
290 name
, co
, filename
, lineno
);
291 return PyString_FromString(buf
);
295 code_compare(PyCodeObject
*co
, PyCodeObject
*cp
)
298 cmp
= PyObject_Compare(co
->co_name
, cp
->co_name
);
300 cmp
= co
->co_argcount
- cp
->co_argcount
;
301 if (cmp
) goto normalize
;
302 cmp
= co
->co_nlocals
- cp
->co_nlocals
;
303 if (cmp
) goto normalize
;
304 cmp
= co
->co_flags
- cp
->co_flags
;
305 if (cmp
) goto normalize
;
306 cmp
= co
->co_firstlineno
- cp
->co_firstlineno
;
307 if (cmp
) goto normalize
;
308 cmp
= PyObject_Compare(co
->co_code
, cp
->co_code
);
310 cmp
= PyObject_Compare(co
->co_consts
, cp
->co_consts
);
312 cmp
= PyObject_Compare(co
->co_names
, cp
->co_names
);
314 cmp
= PyObject_Compare(co
->co_varnames
, cp
->co_varnames
);
316 cmp
= PyObject_Compare(co
->co_freevars
, cp
->co_freevars
);
318 cmp
= PyObject_Compare(co
->co_cellvars
, cp
->co_cellvars
);
331 code_richcompare(PyObject
*self
, PyObject
*other
, int op
)
333 PyCodeObject
*co
, *cp
;
337 if ((op
!= Py_EQ
&& op
!= Py_NE
) ||
338 !PyCode_Check(self
) ||
339 !PyCode_Check(other
)) {
341 /* Py3K warning if types are not equal and comparison
343 if (PyErr_WarnPy3k("code inequality comparisons not supported "
348 Py_INCREF(Py_NotImplemented
);
349 return Py_NotImplemented
;
352 co
= (PyCodeObject
*)self
;
353 cp
= (PyCodeObject
*)other
;
355 eq
= PyObject_RichCompareBool(co
->co_name
, cp
->co_name
, Py_EQ
);
356 if (eq
<= 0) goto unequal
;
357 eq
= co
->co_argcount
== cp
->co_argcount
;
358 if (!eq
) goto unequal
;
359 eq
= co
->co_nlocals
== cp
->co_nlocals
;
360 if (!eq
) goto unequal
;
361 eq
= co
->co_flags
== cp
->co_flags
;
362 if (!eq
) goto unequal
;
363 eq
= co
->co_firstlineno
== cp
->co_firstlineno
;
364 if (!eq
) goto unequal
;
365 eq
= PyObject_RichCompareBool(co
->co_code
, cp
->co_code
, Py_EQ
);
366 if (eq
<= 0) goto unequal
;
367 eq
= PyObject_RichCompareBool(co
->co_consts
, cp
->co_consts
, Py_EQ
);
368 if (eq
<= 0) goto unequal
;
369 eq
= PyObject_RichCompareBool(co
->co_names
, cp
->co_names
, Py_EQ
);
370 if (eq
<= 0) goto unequal
;
371 eq
= PyObject_RichCompareBool(co
->co_varnames
, cp
->co_varnames
, Py_EQ
);
372 if (eq
<= 0) goto unequal
;
373 eq
= PyObject_RichCompareBool(co
->co_freevars
, cp
->co_freevars
, Py_EQ
);
374 if (eq
<= 0) goto unequal
;
375 eq
= PyObject_RichCompareBool(co
->co_cellvars
, cp
->co_cellvars
, Py_EQ
);
376 if (eq
<= 0) goto unequal
;
398 code_hash(PyCodeObject
*co
)
400 long h
, h0
, h1
, h2
, h3
, h4
, h5
, h6
;
401 h0
= PyObject_Hash(co
->co_name
);
402 if (h0
== -1) return -1;
403 h1
= PyObject_Hash(co
->co_code
);
404 if (h1
== -1) return -1;
405 h2
= PyObject_Hash(co
->co_consts
);
406 if (h2
== -1) return -1;
407 h3
= PyObject_Hash(co
->co_names
);
408 if (h3
== -1) return -1;
409 h4
= PyObject_Hash(co
->co_varnames
);
410 if (h4
== -1) return -1;
411 h5
= PyObject_Hash(co
->co_freevars
);
412 if (h5
== -1) return -1;
413 h6
= PyObject_Hash(co
->co_cellvars
);
414 if (h6
== -1) return -1;
415 h
= h0
^ h1
^ h2
^ h3
^ h4
^ h5
^ h6
^
416 co
->co_argcount
^ co
->co_nlocals
^ co
->co_flags
;
421 /* XXX code objects need to participate in GC? */
423 PyTypeObject PyCode_Type
= {
424 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
426 sizeof(PyCodeObject
),
428 (destructor
)code_dealloc
, /* tp_dealloc */
432 (cmpfunc
)code_compare
, /* tp_compare */
433 (reprfunc
)code_repr
, /* tp_repr */
434 0, /* tp_as_number */
435 0, /* tp_as_sequence */
436 0, /* tp_as_mapping */
437 (hashfunc
)code_hash
, /* tp_hash */
440 PyObject_GenericGetAttr
, /* tp_getattro */
442 0, /* tp_as_buffer */
443 Py_TPFLAGS_DEFAULT
, /* tp_flags */
444 code_doc
, /* tp_doc */
447 code_richcompare
, /* tp_richcompare */
448 0, /* tp_weaklistoffset */
452 code_memberlist
, /* tp_members */
456 0, /* tp_descr_get */
457 0, /* tp_descr_set */
458 0, /* tp_dictoffset */
461 code_new
, /* tp_new */
464 /* All about c_lnotab.
466 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
467 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
468 to source code line #s (when needed for tracebacks) via c_lnotab instead.
469 The array is conceptually a list of
470 (bytecode offset increment, line number increment)
471 pairs. The details are important and delicate, best illustrated by example:
473 byte code offset source code line number
480 The first trick is that these numbers aren't stored, only the increments
481 from one row to the next (this doesn't really work, but it's a start):
483 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
485 The second trick is that an unsigned byte can't hold negative values, or
486 values larger than 255, so (a) there's a deep assumption that byte code
487 offsets and their corresponding line #s both increase monotonically, and (b)
488 if at least one column jumps by more than 255 from one row to the next, more
489 than one pair is written to the table. In case #b, there's no way to know
490 from looking at the table later how many were written. That's the delicate
491 part. A user of c_lnotab desiring to find the source line number
492 corresponding to a bytecode address A should do something like this
495 for addr_incr, line_incr in c_lnotab:
501 In order for this to work, when the addr field increments by more than 255,
502 the line # increment in each pair generated must be 0 until the remaining addr
503 increment is < 256. So, in the example above, com_set_lineno should not (as
504 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
505 255, 0, 45, 255, 0, 45.
509 PyCode_Addr2Line(PyCodeObject
*co
, int addrq
)
511 int size
= PyString_Size(co
->co_lnotab
) / 2;
512 unsigned char *p
= (unsigned char*)PyString_AsString(co
->co_lnotab
);
513 int line
= co
->co_firstlineno
;
515 while (--size
>= 0) {
525 Check whether the current instruction is at the start of a line.
529 /* The theory of SET_LINENO-less tracing.
531 In a nutshell, we use the co_lnotab field of the code object
532 to tell when execution has moved onto a different line.
534 As mentioned above, the basic idea is so set things up so
537 *instr_lb <= frame->f_lasti < *instr_ub
539 is true so long as execution does not change lines.
541 This is all fairly simple. Digging the information out of
542 co_lnotab takes some work, but is conceptually clear.
544 Somewhat harder to explain is why we don't *always* call the
545 line trace function when the above test fails.
555 which compiles to this:
558 3 JUMP_IF_FALSE 9 (to 15)
564 12 JUMP_FORWARD 6 (to 21)
567 5 16 LOAD_CONST 2 (2)
570 >> 21 LOAD_CONST 0 (None)
573 If 'a' is false, execution will jump to instruction at offset
574 15 and the co_lnotab will claim that execution has moved to
575 line 3. This is at best misleading. In this case we could
576 associate the POP_TOP with line 4, but that doesn't make
577 sense in all cases (I think).
579 What we do is only call the line trace function if the co_lnotab
580 indicates we have jumped to the *start* of a line, i.e. if the
581 current instruction offset matches the offset given for the
582 start of a line by the co_lnotab.
584 This also takes care of the situation where 'a' is true.
585 Execution will jump from instruction offset 12 to offset 21.
586 Then the co_lnotab would imply that execution has moved to line
587 5, which is again misleading.
589 Why do we set f_lineno when tracing? Well, consider the code
590 above when 'a' is true. If stepping through this with 'n' in
591 pdb, you would stop at line 1 with a "call" type event, then
592 line events on lines 2 and 3, then a "return" type event -- but
593 you would be shown line 5 during this event. This is a change
594 from the behaviour in 2.2 and before, and I've found it
595 confusing in practice. By setting and using f_lineno when
596 tracing, one can report a line number different from that
597 suggested by f_lasti on this one occasion where it's desirable.
602 PyCode_CheckLineNumber(PyCodeObject
* co
, int lasti
, PyAddrPair
*bounds
)
604 int size
, addr
, line
;
607 p
= (unsigned char*)PyString_AS_STRING(co
->co_lnotab
);
608 size
= PyString_GET_SIZE(co
->co_lnotab
) / 2;
611 line
= co
->co_firstlineno
;
614 /* possible optimization: if f->f_lasti == instr_ub
615 (likely to be a common case) then we already know
616 instr_lb -- if we stored the matching value of p
617 somwhere we could skip the first while loop. */
619 /* see comments in compile.c for the description of
620 co_lnotab. A point to remember: increments to p
621 should come in pairs -- although we don't care about
622 the line increments here, treating them as byte
623 increments gets confusing, to say the least. */
625 bounds
->ap_lower
= 0;
627 if (addr
+ *p
> lasti
)
631 bounds
->ap_lower
= addr
;
636 /* If lasti and addr don't match exactly, we don't want to
637 change the lineno slot on the frame or execute a trace
638 function. Return -1 instead.
644 while (--size
>= 0) {
649 bounds
->ap_upper
= addr
;
652 bounds
->ap_upper
= INT_MAX
;