PyDict_*() refcount assumptions
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Nov 1 01:14:17 EDT 2007
En Wed, 31 Oct 2007 22:01:53 -0300, <sndive at gmail.com> escribió:
> i checked this:
> http://svn.python.org/projects/python/trunk/Doc/data/refcounts.dat
> and it seems that
> PyDict_SetItem incref the value being added and the key
> and PyDict_DelItem does not decrement any refcounts
> so i have to do so manually for the key and for the data( by calling
> PyDict_GetItem first).
Looking at the implementation, PyDict_DelItem decrements both the previous
key *and* its associated value. The stored key might not be the same as
the parameter; consider this example:
d = {}
d[1] = 'a'
del d[1.0]
assert d == {}
I think the refcounts.dat file is more intended for automatic tools than
for humans. It cannot express the fact that PyDict_DelItem decrements both
the stored key and its associated value, by example. This is a limitation
of the chosen format. PyDict_Clear decrements a lot of references but
there is no way to express that in the refcounts.dat file.
> Did i decipher the dat file correctly and is there a wrapper for
> DelItem that reverses
> the refcounts to what they were before the SetItem or do i have have
> to do 2 calls for
> deletion?
You don't need any wrapper: PyDict_SetItem and PyDict_DelItem do "the
right thing" with their reference counts, so there is no need of
additional increments/decrements.
--
Gabriel Genellina
More information about the Python-list
mailing list