Ref Count Checking

Robin Becker robin at
Sat Aug 25 17:05:03 CEST 2001

As part of an extension debugging exercise I printed refcounts
relating to the structure
('a1', {'z1': 'az1'}, ['b1', ('c1', {'y1': 'ay1'}, ['d1'], None), 'e1'], None)
either created directly in Python or by my extension.

An earlier pass revealed values of decrementing values 
for the None output from the extension (it was stealing a None).
After Correcting I'm still getting differences.
In particular the strings az1, ay1, b1, c1, d1, e1 seem to have counts from the
extension which are 3 lower than from the python version.

Is there any way to deduce what the python count should be? 
I'm assuming that a string like az1 is reasonably unique so there
shouldn't be any question of a library reference.

('a1', {'z1': 'az1'}, ['b1', ('c1', {'y1': 'ay1'}, ['d1'], None), 'e1'], None)
T6 a1:10 D7 z1:9 az1:7 L7 b1:10 T7 c1:10 D7 y1:9 ay1:7 L7 d1:10 N209 e1:10 N209
('a1', {'z1': 'az1'}, ['b1', ('c1', {'y1': 'ay1'}, ['d1'], None), 'e1'], None)
T6 a1:7 D7 z1:9 az1:4 L7 b1:7 T7 c1:7 D7 y1:9 ay1:4 L7 d1:7 N209 e1:7 N209

import ext
import sys, string
from types import StringType, DictType, TupleType, ListType
def TL(ob):
        C = ob is None and 'N' or {StringType:'S', DictType:'D', ListType:'L', TupleType: 'T'}[type(ob)]
        if C =='S': C = ob + ':'
        return '%s%d' % (C, sys.getrefcount(ob))

def RC(ob,L=None):
        if L is None: L = []

        t = type(ob)
        if t in (ListType,TupleType):
                for i in ob: RC(i,L)
        elif t is DictType:
                K = ob.keys()
                for k in K:
        return L

p=('a1', {'z1': 'az1'}, ['b1', ('c1', {'y1': 'ay1'}, ['d1'], None), 'e1'], None)
print p
print string.join(RC(p))
del p

print p
print string.join(RC(p))
Robin Becker

More information about the Python-list mailing list