diff options
author | Guido van Rossum <guido@python.org> | 2006-02-25 22:38:04 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-02-25 22:38:04 (GMT) |
commit | 1968ad32cd7f46d9bb64826672ef68cdaee35288 (patch) | |
tree | c46db5a446d9de18fb8436408ec29d2111a2f5ad /Objects/dictobject.c | |
parent | ab51f5f24d6f6edef5e8fac5e31b2e4ac0cbdbac (diff) | |
download | cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.zip cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.tar.gz cpython-1968ad32cd7f46d9bb64826672ef68cdaee35288.tar.bz2 |
- Patch 1433928:
- The copy module now "copies" function objects (as atomic objects).
- dict.__getitem__ now looks for a __missing__ hook before raising
KeyError.
- Added a new type, defaultdict, to the collections module.
This uses the new __missing__ hook behavior added to dict (see above).
Diffstat (limited to 'Objects/dictobject.c')
-rw-r--r-- | Objects/dictobject.c | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c index f5e5320..2254762 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -882,8 +882,22 @@ dict_subscript(dictobject *mp, register PyObject *key) return NULL; } v = (mp->ma_lookup)(mp, key, hash) -> me_value; - if (v == NULL) + if (v == NULL) { + if (!PyDict_CheckExact(mp)) { + /* Look up __missing__ method if we're a subclass. */ + static PyObject *missing_str = NULL; + if (missing_str == NULL) + missing_str = + PyString_InternFromString("__missing__"); + PyObject *missing = _PyType_Lookup(mp->ob_type, + missing_str); + if (missing != NULL) + return PyObject_CallFunctionObjArgs(missing, + (PyObject *)mp, key, NULL); + } PyErr_SetObject(PyExc_KeyError, key); + return NULL; + } else Py_INCREF(v); return v; |