Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > PyDict_*() refcount assumptions

Reply
Thread Tools

PyDict_*() refcount assumptions

 
 
sndive@gmail.com
Guest
Posts: n/a
 
      11-01-2007
i checked this: http://svn.python.org/projects/pytho.../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?

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      11-01-2007
En Wed, 31 Oct 2007 22:01:53 -0300, <(E-Mail Removed)> escribió:

> i checked this:
> http://svn.python.org/projects/pytho.../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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shed Skin - Does it break any Python assumptions? skip@pobox.com Python 0 12-18-2006 02:07 PM
beginner's refcount questions Jens Theisen Python 3 10-30-2006 07:07 AM
Assumptions About the Size of Integer Types Lash Rambo C++ 7 06-22-2004 09:08 AM
Debugging leaking refcount Jean-François Doyon Python 0 05-14-2004 06:08 PM
[Q] Extension: Refcount for exception types Ames Andreas (MPA/DF) Python 1 04-16-2004 07:55 PM



Advertisments