[Python-Dev] calling dictresize outside dictobject.c (original) (raw)

Benjamin Peterson benjamin at python.org
Thu Apr 9 21:17:39 CEST 2009


Hi Dan, Thanks for your interest.

2009/4/6 Dan Schult <dschult at colgate.edu>:

Hi, 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->malookup(). 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 PyDictSetItem.  But I can't call dictresize to finish off the process. Should I be using PyDictSetItem directly?  No... it does its own lookup. I don't want a second lookup!   I already know which entry will be filled. 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?

Because it's not useful outside the intimate implementation details of dictobject.c

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

Raymond tells me the cost of the second lookup is negligible because of caching, but PyObject_Hash needn't be called two times. He's working on a patch later today.

-- Regards, Benjamin



More information about the Python-Dev mailing list