refcount differences in 2.5

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed May 16 19:15:59 EDT 2007


Hi

With Python 2.5, there is a change on the reference count of objects  
compared with previous versions (as reported by sys.getrefcount).

All Python versions from 2.1 thru 2.4 gave these same results:

Python 2.1.3 (#35, Apr  8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getrefcount(11111111)
2
>>> sys.getrefcount(11111111+1)
1
>>> x=22222222
>>> sys.getrefcount(x)
2
>>> sys.getrefcount(x+1)
1
>>> sys.getrefcount("A unique *str-ing*")
2
>>> sys.getrefcount("A unique *str-ing*".upper())
1
>>> sys.getrefcount(3+2j)
1
>>> class W: pass
...
>>> x=W()
>>> sys.getrefcount(x)
2
>>> sys.getrefcount(W())
1
>>>

But Python 2.5 (2.5.1 not tested yet) gives different results:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]  
on win
32
>>> import sys
>>> sys.getrefcount(11111111)
3
>>> sys.getrefcount(11111111+1)
2
>>> x=22222222
>>> sys.getrefcount(x)
2
>>> sys.getrefcount(x+1)
1
>>> sys.getrefcount("A unique *str-ing*")
3
>>> sys.getrefcount("A unique *str-ing*".upper())
1
>>> sys.getrefcount(3+2j)
2
>>> class W: pass
...
>>> x=W()
>>> sys.getrefcount(x)
2
>>> sys.getrefcount(W())
1
>>>

In particular, I wonder why getrefcount(12341234) returns 3 instead of 2 -  
who is holding the extra reference?

My main two concerns are:
- is there a reference leak somewhere?
- it's a bit harder to debug my own references since I don't know  
beforehand the value I should expect from sys.getrefcount()

-- 
Gabriel Genellina




More information about the Python-list mailing list