Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2dff991

Browse files
committed
Give code objects a more useful representation.
1 parent 97f0277 commit 2dff991

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

Python/compile.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ code_dealloc(co)
7272
DEL(co);
7373
}
7474

75+
static object *
76+
code_repr(co)
77+
codeobject *co;
78+
{
79+
char buf[500];
80+
int lineno = -1;
81+
char *p = GETSTRINGVALUE(co->co_code);
82+
char *filename = "???";
83+
if (*p == SET_LINENO)
84+
lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
85+
if (co->co_filename && is_stringobject(co->co_filename))
86+
filename = getstringvalue(co->co_filename);
87+
sprintf(buf, "<code object at %lx, file \"%.400s\", line %d>",
88+
(long)co, filename, lineno);
89+
return newstringobject(buf);
90+
}
91+
7592
typeobject Codetype = {
7693
OB_HEAD_INIT(&Typetype)
7794
0,
@@ -83,7 +100,7 @@ typeobject Codetype = {
83100
code_getattr, /*tp_getattr*/
84101
0, /*tp_setattr*/
85102
0, /*tp_compare*/
86-
0, /*tp_repr*/
103+
code_repr, /*tp_repr*/
87104
0, /*tp_as_number*/
88105
0, /*tp_as_sequence*/
89106
0, /*tp_as_mapping*/

0 commit comments

Comments
 (0)