PyDict_*() refcount assumptions

Discussion in 'Python' started by sndive@gmail.com, Nov 1, 2007.

  1. Guest

    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?
    , Nov 1, 2007
    #1
    1. Advertising

  2. En Wed, 31 Oct 2007 22:01:53 -0300, <> 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
    Gabriel Genellina, Nov 1, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lash Rambo
    Replies:
    7
    Views:
    4,722
    David Harmon
    Jun 22, 2004
  2. Ames Andreas (MPA/DF)

    [Q] Extension: Refcount for exception types

    Ames Andreas (MPA/DF), Apr 16, 2004, in forum: Python
    Replies:
    1
    Views:
    277
    =?windows-1252?Q?=22Martin_v=2E_L=F6wis=22?=
    Apr 16, 2004
  3. Jean-François Doyon

    Debugging leaking refcount

    Jean-François Doyon, May 14, 2004, in forum: Python
    Replies:
    0
    Views:
    287
    Jean-François Doyon
    May 14, 2004
  4. Jens Theisen

    beginner's refcount questions

    Jens Theisen, Oct 30, 2006, in forum: Python
    Replies:
    3
    Views:
    259
    Fredrik Lundh
    Oct 30, 2006
  5. Replies:
    0
    Views:
    337
Loading...

Share This Page