Skip to content

Commit

Permalink
Implement relate for code object and fix its test in test_Path.py
Browse files Browse the repository at this point in the history
Some referenced attributes are no longer slotted. I'm not sure
how to get a fallback to default relate for slotted attributes
so I'm just going to make relate do the entire thing.

For #41
  • Loading branch information
zhuyifei1999 committed May 13, 2023
1 parent cf9efa6 commit 6f91e4c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 6 additions & 2 deletions guppy/heapy/test/test_Path.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ def f():
# xxx brittle test but catches a bug
self.chkpath(co, 3, '%s.co_consts[1]')
# commented in notes Sep 27 2004
relAttr = ('co_code', 'co_consts', 'co_names', 'co_varnames',
'co_freevars', 'co_cellvars', 'co_filename', 'co_name')
relAttr = ('co_code', 'co_consts', 'co_names',
'co_filename', 'co_name')
if self.version_info < (3, 11):
relAttr += ('co_varnames', 'co_freevars', 'co_cellvars')
else:
relAttr += ('co_exceptiontable', 'co_qualname')
if self.version_info >= (3, 10):
relAttr += ('co_linetable',)
else:
Expand Down
38 changes: 37 additions & 1 deletion src/heapy/stdtypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,42 @@ code_traverse(NyHeapTraverse *ta) {
return 0;
}

static int
code_relate(NyHeapRelate *r)
{
PyCodeObject *v = (void *)r->src;
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
RENAMEATTR(_co_code, co_code);
#else
ATTR(co_code);
#endif
ATTR(co_consts);
ATTR(co_names);
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ATTR(co_exceptiontable);
#endif
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
INTERATTR(co_localsplusnames);
INTERATTR(co_localspluskinds);
#else
ATTR(co_varnames);
ATTR(co_freevars);
ATTR(co_cellvars);
#endif
ATTR(co_filename);
ATTR(co_name);
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 11
ATTR(co_qualname);
#endif
#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 10
ATTR(co_linetable);
#else
ATTR(co_lnotab);
#endif
ATTR(co_weakreflist);
return 0;
}

/* type_traverse adapted from typeobject.c from 2.4.2
except:
* I removed the check for heap type
Expand Down Expand Up @@ -542,7 +578,7 @@ NyHeapDef NyStdTypes_HeapDef[] = {
0, /* type */
0, /* size */
code_traverse, /* traverse */
0 /* relate */
code_relate /* relate */
}, {
0, /* flags */
0, /* type */
Expand Down

0 comments on commit 6f91e4c

Please sign in to comment.