Hi Dan,
Thanks for your interest.

2009/4/6 Dan Schult <dsch...@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->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 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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to