[Python-Dev] calling dictresize outside dictobject.c

Dan Schult dschult at colgate.edu
Tue Apr 7 05:47:17 CEST 2009

I'm trying to write a C extension which is a subclass of dict.
I want to do something like a setdefault() but with a single lookup.

Looking through the dictobject code, the three workhorse
routines lookdict, insertdict and dictresize are not available
directly for functions outside dictobject.c,
but I can get at lookdict through dict->ma_lookup().

So I use lookdict to get the PyDictEntry (call it ep) I'm looking for.
The comments for lookdict say ep is ready to be set... so I do that.
Then I check whether the dict needs to be resized--following the
nice example of PyDict_SetItem.  But I can't call dictresize to finish
off the process.

Should I be using PyDict_SetItem directly?  No... it does its own  
I don't want a second lookup!   I already know which entry will be  

So then I look at the code for setdefault and it also does
a double lookup for checking and setting an entry.

What subtle issue am I missing?
Why does setdefault do a double lookup?
More globally, why isn't dictresize available through the C-API?

If there isn't a reason to do a double lookup I have a patch for  
but I thought I should ask here first.


More information about the Python-Dev mailing list