PyDict_*() refcount assumptions

S

sndive

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).
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?
 
G

Gabriel Genellina

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top