[Python-Dev] PyDict_GetItem & dict_subscript

Thomas Wouters thomas@xs4all.net
Wed, 14 Nov 2001 12:58:10 +0100


On Wed, Nov 14, 2001 at 12:20:18PM +0100, Thomas Heller wrote:
> From: "Thomas Wouters" <thomas@xs4all.net>

> > While trying to figure out why passing an unhashable object to switch didn't
> > cause a TypeError: unhashable object, I found out PyDict_GetItem and
> > dict_subscript behave differently in that respect. PyDict_GetItem does:
> > 
> >                 hash = PyObject_Hash(key);
> >                 if (hash == -1) {
> >                         PyErr_Clear();   
> >                         return NULL;
> >                 }
> > 
> > whereas dict_subscript does:
> > 
> >                 hash = PyObject_Hash(key);
> >                 if (hash == -1)
> >                         return NULL;
> > 
> > PyDict_SetItem and PyDict_DelItem both behave like dict_subscript (that is,
> > they propagate the error upwards, instead of clearing the error.) Is this
> > intentional ?

> It seems so, at least it's documented.

Is it ? Where ? The API docs (both devel and current versions) have only
this to say:

PyObject* PyDict_GetItem(PyObject *p, PyObject *key)
    Return value: Borrowed reference.
Returns the object from dictionary p which has a key 'key'. Returns NULL if
the key 'key' is not present, but without setting an exception.

> I'd use PyObject_GetItem(), but...

Hm, I guess. Not quite intuitive to me, but it'll do.

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!