Rationale ========= A frozendict type is a common request from users and there are various implementations. There are two main Python implementations:
* "blacklist": frozendict inheriting from dict and overriding methods to raise an exception when trying to modify the frozendict * "whitelist": frozendict not inheriting from dict and only implement some dict methods, or implement all dict methods but raise exceptions when trying to modify the frozendict The blacklist implementation has a major issue: it is still possible to call write methods of the dict class (e.g. dict.set(my_frozendict, key, value)). The whitelist implementation has an issue: frozendict and dict are not "compatible", dict is not a subclass of frozendict (and frozendict is not a subclass of dict). I propose to add a new frozendict builtin type and make dict type inherits from it. frozendict would not have methods to modify its content and values must be immutable. Constraints =========== * frozendict values must be immutable, as dict keys * frozendict can be used with the C API of the dict object (e.g. PyDict_GetItem) but write methods (e.g. PyDict_SetItem) would fail with a TypeError ("expect dict, got frozendict") * frozendict.__hash__() has to be determinist * frozendict has not the following methods: clear, __delitem__, pop, popitem, setdefault, __setitem__ and update. As tuple/frozenset has less methods than list/set. * issubclass(dict, frozendict) is True, whereas issubclass(frozendict, dict) is False Implementation ============== * Add an hash field to the PyDictObject structure * Make dict inherits from frozendict * frozendict values are checked for immutability property by calling their __hash__ method, with a fast-path for known immutable types (int, float, bytes, str, tuple, frozenset) * frozendict.__hash__ computes hash(frozenset(self.items())) and caches the result is its private hash attribute Attached patch is a work-in-progress implementation. TODO ==== * Add a frozendict abstract base class to collections? * frozendict may not overallocate dictionary buckets? -- Examples of frozendict implementations: http://bob.pythonmac.org/archives/2005/03/04/frozendict/ http://code.activestate.com/recipes/498072-implementing-an-immutable-dictionary/ http://code.activestate.com/recipes/414283-frozen-dictionaries/ http://corebio.googlecode.com/svn/trunk/apidocs/corebio.utils.frozendict-class.html http://code.google.com/p/lingospot/source/browse/trunk/frozendict/frozendict.py http://cmssdt.cern.ch/SDT/doxygen/CMSSW_4_4_2/doc/html/d6/d2f/classfrozendict_1_1frozendict.html See also the recent discussion on python-list: http://mail.python.org/pipermail/python-list/2012-February/1287658.html -- See also the PEP 351. Victor
diff --git a/Include/dictobject.h b/Include/dictobject.h --- a/Include/dictobject.h +++ b/Include/dictobject.h @@ -84,10 +84,14 @@ struct _dictobject { PyDictEntry *ma_table; PyDictEntry *(*ma_lookup)(PyDictObject *mp, PyObject *key, Py_hash_t hash); PyDictEntry ma_smalltable[PyDict_MINSIZE]; + + /* only used by frozendict */ + Py_hash_t hash; }; #endif /* Py_LIMITED_API */ PyAPI_DATA(PyTypeObject) PyDict_Type; +PyAPI_DATA(PyTypeObject) PyFrozenDict_Type; PyAPI_DATA(PyTypeObject) PyDictIterKey_Type; PyAPI_DATA(PyTypeObject) PyDictIterValue_Type; PyAPI_DATA(PyTypeObject) PyDictIterItem_Type; @@ -97,6 +101,10 @@ PyAPI_DATA(PyTypeObject) PyDictValues_Ty #define PyDict_Check(op) \ PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_DICT_SUBCLASS) +#define PyFrozenDict_Check(op) \ + (PyDict_Check(op) || \ + Py_TYPE(op) == &PyFrozenDict_Type || \ + PyType_IsSubtype(Py_TYPE(op), &PyFrozenDict_Type)) #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type) #define PyDictKeys_Check(op) (Py_TYPE(op) == &PyDictKeys_Type) #define PyDictItems_Check(op) (Py_TYPE(op) == &PyDictItems_Type) diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py --- a/Lib/test/test_dict.py +++ b/Lib/test/test_dict.py @@ -788,11 +788,54 @@ class Dict(dict): class SubclassMappingTests(mapping_tests.BasicTestMappingProtocol): type2test = Dict + +class FrozenDictTests(unittest.TestCase): + def test_inherance(self): + self.assertIsInstance(dict(), + frozendict) + self.assertNotIsInstance(frozendict(), + dict) + + def test_copy(self): + self.assertIsInstance(frozendict().copy(), + frozendict) + + def test_hash(self): + self.assertEqual(hash(frozendict()), + hash(frozenset())) + self.assertEqual(hash(frozendict({1: 2})), + hash(frozenset({(1, 2)}))) + + def test_repr(self): + self.assertEqual(repr(frozendict()), "frozendict({})") + self.assertEqual(repr(frozendict(x=1)), "frozendict({'x': 1})") + + def test_readonly(self): + d = frozendict() + for method in ('__setitem__', 'pop', 'popitem', 'setdefault', 'update'): + self.assertFalse(hasattr(d, method)) + class MyType: + def __hash__(self): + raise TypeError("not hashable") + for not_hashable in ([], {}, MyType()): + self.assertRaises(TypeError, frozendict, key=not_hashable) + self.assertRaises(TypeError, frozendict, [(not_hashable, 0)]) + self.assertRaises(TypeError, frozendict, [(0, not_hashable)]) + + def test_valid(self): + class MyType: + pass + for hashable in (5, 1.0, "abc", (1, 2), MyType()): + frozendict(key=hashable) + frozendict(hashable=0) + + def test_main(): support.run_unittest( DictTest, GeneralMappingTests, SubclassMappingTests, + FrozenDictTests, ) if __name__ == "__main__": diff --git a/Objects/dictobject.c b/Objects/dictobject.c --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -10,7 +10,6 @@ #include "Python.h" #include "stringlib/eq.h" - /* Set a key error with the specified argument, wrapping it in a * tuple automatically so that tuple keys are not unpacked as the * exception arguments. */ @@ -25,6 +24,12 @@ set_key_error(PyObject *arg) Py_DECREF(tup); } +static int +dict_merge(PyObject *a, PyObject *b, int override, int is_frozen); +static int +dict_mergefromseq2(PyObject *d, PyObject *seq2, int override, int is_frozen); +static PyObject *frozendict_new(void); + /* Define this out if you don't want conversion statistics on exit. */ #undef SHOW_CONVERSION_COUNTS @@ -461,7 +466,7 @@ _PyDict_HasOnlyStringKeys(PyObject *dict { Py_ssize_t pos = 0; PyObject *key, *value; - assert(PyDict_Check(dict)); + assert(PyFrozenDict_Check(dict)); /* Shortcut */ if (((PyDictObject *)dict)->ma_lookup == lookdict_unicode) return 1; @@ -725,7 +730,7 @@ PyDict_GetItem(PyObject *op, PyObject *k PyDictObject *mp = (PyDictObject *)op; PyDictEntry *ep; PyThreadState *tstate; - if (!PyDict_Check(op)) + if (!PyFrozenDict_Check(op)) return NULL; if (!PyUnicode_CheckExact(key) || (hash = ((PyASCIIObject *) key)->hash) == -1) @@ -775,7 +780,7 @@ PyDict_GetItemWithError(PyObject *op, Py PyDictObject*mp = (PyDictObject *)op; PyDictEntry *ep; - if (!PyDict_Check(op)) { + if (!PyFrozenDict_Check(op)) { PyErr_BadInternalCall(); return NULL; } @@ -840,6 +845,27 @@ dict_set_item_by_hash_or_entry(register * remove them. */ int +basedict_setitem(PyObject *op, PyObject *key, PyObject *value) +{ + register Py_hash_t hash; + + assert(PyFrozenDict_Check(op)); + assert(key); + assert(value); + if (PyUnicode_CheckExact(key)) { + hash = ((PyASCIIObject *) key)->hash; + if (hash == -1) + hash = PyObject_Hash(key); + } + else { + hash = PyObject_Hash(key); + if (hash == -1) + return -1; + } + return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value); +} + +int PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) { register Py_hash_t hash; @@ -913,9 +939,10 @@ PyDict_Clear(PyObject *op) Py_ssize_t i, n; #endif - if (!PyDict_Check(op)) + if (!PyFrozenDict_Check(op)) return; mp = (PyDictObject *)op; + #ifdef Py_DEBUG n = mp->ma_mask + 1; i = 0; @@ -992,7 +1019,7 @@ PyDict_Next(PyObject *op, Py_ssize_t *pp register Py_ssize_t mask; register PyDictEntry *ep; - if (!PyDict_Check(op)) + if (!PyFrozenDict_Check(op)) return 0; i = *ppos; if (i < 0) @@ -1019,7 +1046,7 @@ _PyDict_Next(PyObject *op, Py_ssize_t *p register Py_ssize_t mask; register PyDictEntry *ep; - if (!PyDict_Check(op)) + if (!PyFrozenDict_Check(op)) return 0; i = *ppos; if (i < 0) @@ -1143,6 +1170,18 @@ Done: return result; } +static PyObject * +frozendict_repr(PyDictObject *mp) +{ + PyObject *repr, *result; + repr = dict_repr(mp); + if (repr == NULL) + return NULL; + result = PyUnicode_FromFormat("frozendict(%S)", repr); + Py_DECREF(repr); + return result; +} + static Py_ssize_t dict_length(PyDictObject *mp) { @@ -1198,6 +1237,11 @@ dict_ass_sub(PyDictObject *mp, PyObject return PyDict_SetItem((PyObject *)mp, v, w); } +static PyMappingMethods frozendict_as_mapping = { + (lenfunc)dict_length, /*mp_length*/ + (binaryfunc)dict_subscript, /*mp_subscript*/ +}; + static PyMappingMethods dict_as_mapping = { (lenfunc)dict_length, /*mp_length*/ (binaryfunc)dict_subscript, /*mp_subscript*/ @@ -1419,7 +1463,8 @@ Fail: } static int -dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) +dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, + char *methname, int is_frozen) { PyObject *arg = NULL; int result = 0; @@ -1430,13 +1475,13 @@ dict_update_common(PyObject *self, PyObj else if (arg != NULL) { _Py_IDENTIFIER(keys); if (_PyObject_HasAttrId(arg, &PyId_keys)) - result = PyDict_Merge(self, arg, 1); + result = dict_merge(self, arg, 1, is_frozen); else - result = PyDict_MergeFromSeq2(self, arg, 1); + result = dict_mergefromseq2(self, arg, 1, is_frozen); } if (result == 0 && kwds != NULL) { if (PyArg_ValidateKeywordArguments(kwds)) - result = PyDict_Merge(self, kwds, 1); + result = dict_merge(self, kwds, 1, is_frozen); else result = -1; } @@ -1446,11 +1491,24 @@ dict_update_common(PyObject *self, PyObj static PyObject * dict_update(PyObject *self, PyObject *args, PyObject *kwds) { - if (dict_update_common(self, args, kwds, "update") != -1) + if (dict_update_common(self, args, kwds, "update", 0) != -1) Py_RETURN_NONE; return NULL; } +static int +check_immutable(PyObject *obj) +{ + /* fast-path for common known immutable types */ + if (PyLong_Check(obj) || PyFloat_Check(obj) || PyUnicode_Check(obj) + || PyTuple_Check(obj)) + return 0; + if (PyObject_Hash(obj) != -1) + return 0; + else + return -1; +} + /* Update unconditionally replaces existing items. Merge has a 3rd argument 'override'; if set, it acts like Update, otherwise it leaves existing items unchanged. @@ -1461,8 +1519,8 @@ dict_update(PyObject *self, PyObject *ar producing iterable objects of length 2. */ -int -PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) +static int +dict_mergefromseq2(PyObject *d, PyObject *seq2, int override, int is_frozen) { PyObject *it; /* iter(seq2) */ Py_ssize_t i; /* index into seq2 of current element */ @@ -1470,7 +1528,7 @@ PyDict_MergeFromSeq2(PyObject *d, PyObje PyObject *fast; /* item as a 2-tuple or 2-list */ assert(d != NULL); - assert(PyDict_Check(d)); + assert(PyFrozenDict_Check(d)); assert(seq2 != NULL); it = PyObject_GetIter(seq2); @@ -1511,8 +1569,10 @@ PyDict_MergeFromSeq2(PyObject *d, PyObje /* Update/merge with this (key, value) pair. */ key = PySequence_Fast_GET_ITEM(fast, 0); value = PySequence_Fast_GET_ITEM(fast, 1); + if (is_frozen && check_immutable(value) == -1) + goto Fail; if (override || PyDict_GetItem(d, key) == NULL) { - int status = PyDict_SetItem(d, key, value); + int status = basedict_setitem(d, key, value); if (status < 0) goto Fail; } @@ -1532,13 +1592,19 @@ Return: } int +PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) +{ + return dict_mergefromseq2(d, seq2, override, 0); +} + +int PyDict_Update(PyObject *a, PyObject *b) { - return PyDict_Merge(a, b, 1); + return dict_merge(a, b, 1, 0); } -int -PyDict_Merge(PyObject *a, PyObject *b, int override) +static int +dict_merge(PyObject *a, PyObject *b, int override, int is_frozen) { register PyDictObject *mp, *other; register Py_ssize_t i; @@ -1549,12 +1615,13 @@ PyDict_Merge(PyObject *a, PyObject *b, i * things quite efficiently. For the latter, we only require that * PyMapping_Keys() and PyObject_GetItem() be supported. */ - if (a == NULL || !PyDict_Check(a) || b == NULL) { + if (a == NULL || !PyFrozenDict_Check(a) || b == NULL) { PyErr_BadInternalCall(); return -1; } mp = (PyDictObject*)a; - if (PyDict_Check(b)) { + + if (PyFrozenDict_Check(b)) { other = (PyDictObject*)b; if (other == mp || other->ma_used == 0) /* a.update(a) or a.update({}); nothing to do */ @@ -1575,16 +1642,18 @@ PyDict_Merge(PyObject *a, PyObject *b, i } for (i = 0; i <= other->ma_mask; i++) { entry = &other->ma_table[i]; - if (entry->me_value != NULL && - (override || - PyDict_GetItem(a, entry->me_key) == NULL)) { - Py_INCREF(entry->me_key); - Py_INCREF(entry->me_value); - if (insertdict(mp, entry->me_key, - entry->me_hash, - entry->me_value) != 0) - return -1; - } + if (entry->me_value == NULL) + continue; + if (!override && PyDict_GetItem(a, entry->me_key) != NULL) + continue; + if (is_frozen && check_immutable(entry->me_value) == -1) + return -1; + Py_INCREF(entry->me_key); + Py_INCREF(entry->me_value); + if (insertdict(mp, entry->me_key, + entry->me_hash, + entry->me_value) != 0) + return -1; } } else { @@ -1618,7 +1687,13 @@ PyDict_Merge(PyObject *a, PyObject *b, i Py_DECREF(key); return -1; } - status = PyDict_SetItem(a, key, value); + if (is_frozen && check_immutable(value) == -1) { + Py_DECREF(iter); + Py_DECREF(key); + Py_DECREF(value); + return -1; + } + status = basedict_setitem(a, key, value); Py_DECREF(key); Py_DECREF(value); if (status < 0) { @@ -1634,6 +1709,12 @@ PyDict_Merge(PyObject *a, PyObject *b, i return 0; } +int +PyDict_Merge(PyObject *a, PyObject *b, int override) +{ + return dict_merge(a, b, override, 0); +} + static PyObject * dict_copy(register PyDictObject *mp) { @@ -1652,16 +1733,33 @@ PyDict_Copy(PyObject *o) copy = PyDict_New(); if (copy == NULL) return NULL; - if (PyDict_Merge(copy, o, 1) == 0) + if (dict_merge(copy, o, 1, 0) == 0) return copy; Py_DECREF(copy); return NULL; } +static PyObject * +frozendict_copy(PyObject *o) +{ + PyObject *copy; + if (o == NULL || !PyFrozenDict_Check(o)) { + PyErr_BadInternalCall(); + return NULL; + } + copy = frozendict_new(); + if (copy == NULL) + return NULL; + if (dict_merge(copy, o, 1, 1) == 0) + return copy; + Py_DECREF(copy); + return NULL; +} + Py_ssize_t PyDict_Size(PyObject *mp) { - if (mp == NULL || !PyDict_Check(mp)) { + if (mp == NULL || !PyFrozenDict_Check(mp)) { PyErr_BadInternalCall(); return -1; } @@ -1671,7 +1769,7 @@ PyDict_Size(PyObject *mp) PyObject * PyDict_Keys(PyObject *mp) { - if (mp == NULL || !PyDict_Check(mp)) { + if (mp == NULL || !PyFrozenDict_Check(mp)) { PyErr_BadInternalCall(); return NULL; } @@ -1681,7 +1779,7 @@ PyDict_Keys(PyObject *mp) PyObject * PyDict_Values(PyObject *mp) { - if (mp == NULL || !PyDict_Check(mp)) { + if (mp == NULL || !PyFrozenDict_Check(mp)) { PyErr_BadInternalCall(); return NULL; } @@ -1691,7 +1789,7 @@ PyDict_Values(PyObject *mp) PyObject * PyDict_Items(PyObject *mp) { - if (mp == NULL || !PyDict_Check(mp)) { + if (mp == NULL || !PyFrozenDict_Check(mp)) { PyErr_BadInternalCall(); return NULL; } @@ -1746,7 +1844,7 @@ dict_richcompare(PyObject *v, PyObject * int cmp; PyObject *res; - if (!PyDict_Check(v) || !PyDict_Check(w)) { + if (!PyFrozenDict_Check(v) || !PyFrozenDict_Check(w)) { res = Py_NotImplemented; } else if (op == Py_EQ || op == Py_NE) { @@ -2034,7 +2132,29 @@ PyDoc_STRVAR(items__doc__, PyDoc_STRVAR(values__doc__, "D.values() -> an object providing a view on D's values"); -static PyMethodDef mapp_methods[] = { +static PyMethodDef frozendict_methods[] = { + {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, + contains__doc__}, + {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, + getitem__doc__}, + {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, + sizeof__doc__}, + {"get", (PyCFunction)dict_get, METH_VARARGS, + get__doc__}, + {"keys", (PyCFunction)dictkeys_new, METH_NOARGS, + keys__doc__}, + {"items", (PyCFunction)dictitems_new, METH_NOARGS, + items__doc__}, + {"values", (PyCFunction)dictvalues_new, METH_NOARGS, + values__doc__}, + {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, + fromkeys__doc__}, + {"copy", (PyCFunction)frozendict_copy, METH_NOARGS, + copy__doc__}, + {NULL, NULL} /* sentinel */ +}; + +static PyMethodDef dict_methods[] = { {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, contains__doc__}, {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, @@ -2138,10 +2258,37 @@ dict_new(PyTypeObject *type, PyObject *a return self; } +static PyObject * +frozendict_new(void) +{ + PyObject *self; + PyDictObject *d; + + self = dict_new(&PyFrozenDict_Type, NULL, NULL); + if (self == NULL) + return NULL; + d = (PyDictObject *)self; + d->hash = -1; + return self; +} + +static PyObject * +frozendict___new__(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *self = frozendict_new(); + if (self == NULL) + return NULL; + if (dict_update_common(self, args, kwds, "frozendict", 1) == -1) { + Py_DECREF(self); + return NULL; + } + return self; +} + static int dict_init(PyObject *self, PyObject *args, PyObject *kwds) { - return dict_update_common(self, args, kwds, "dict"); + return dict_update_common(self, args, kwds, "dict", 0); } static PyObject * @@ -2150,6 +2297,86 @@ dict_iter(PyDictObject *dict) return dictiter_new(dict, &PyDictIterKey_Type); } +static Py_hash_t +frozendict_hash(PyObject *self) +{ + PyDictObject *frozendict = (PyDictObject *)self; + PyObject *items, *frozen_items; + Py_hash_t hash; + + if (frozendict->hash != -1) + return frozendict->hash; + + items = PyObject_CallMethod(self, "items", ""); + if (items == NULL) + return -1; + frozen_items = PyFrozenSet_New(items); + Py_DECREF(items); + if (frozen_items == NULL) + return -1; + hash = PyObject_Hash(frozen_items); + Py_DECREF(frozen_items); + if (hash == -1) + return -1; + + frozendict->hash = hash; + return hash; +} + +PyDoc_STRVAR(frozendict_doc, +"frozendict() -> new empty frozen dictionary\n" +"frozendict(mapping) -> new frozen dictionary initialized from a mapping object's\n" +" (key, value) pairs\n" +"frozendict(iterable) -> new frozen dictionary initialized as if via:\n" +" d = {}\n" +" for k, v in iterable:\n" +" d[k] = v\n" +"frozendict(**kwargs) -> new frozen dictionary initialized with the name=value\n" +" pairs in the keyword argument list. For example: frozendict(one=1, two=2)"); + +PyTypeObject PyFrozenDict_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "frozendict", + sizeof(PyDictObject), + 0, + (destructor)dict_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + (reprfunc)frozendict_repr, /* tp_repr */ + 0, /* tp_as_number */ + &dict_as_sequence, /* tp_as_sequence */ + &frozendict_as_mapping, /* tp_as_mapping */ + frozendict_hash, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + PyObject_GenericGetAttr, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + frozendict_doc, /* tp_doc */ + dict_traverse, /* tp_traverse */ + dict_tp_clear, /* tp_clear */ + dict_richcompare, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + (getiterfunc)dict_iter, /* tp_iter */ + 0, /* tp_iternext */ + frozendict_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + PyType_GenericAlloc, /* tp_alloc */ + frozendict___new__, /* tp_new */ + PyObject_GC_Del, /* tp_free */ +}; + PyDoc_STRVAR(dictionary_doc, "dict() -> new empty dictionary\n" "dict(mapping) -> new dictionary initialized from a mapping object's\n" @@ -2190,10 +2417,10 @@ PyTypeObject PyDict_Type = { 0, /* tp_weaklistoffset */ (getiterfunc)dict_iter, /* tp_iter */ 0, /* tp_iternext */ - mapp_methods, /* tp_methods */ + dict_methods, /* tp_methods */ 0, /* tp_members */ 0, /* tp_getset */ - 0, /* tp_base */ + &PyFrozenDict_Type, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ @@ -2324,7 +2551,7 @@ static PyObject *dictiter_iternextkey(di if (d == NULL) return NULL; - assert (PyDict_Check(d)); + assert (PyFrozenDict_Check(d)); if (di->di_used != d->ma_used) { PyErr_SetString(PyExc_RuntimeError, @@ -2396,7 +2623,7 @@ static PyObject *dictiter_iternextvalue( if (d == NULL) return NULL; - assert (PyDict_Check(d)); + assert (PyFrozenDict_Check(d)); if (di->di_used != d->ma_used) { PyErr_SetString(PyExc_RuntimeError, @@ -2468,7 +2695,7 @@ static PyObject *dictiter_iternextitem(d if (d == NULL) return NULL; - assert (PyDict_Check(d)); + assert (PyFrozenDict_Check(d)); if (di->di_used != d->ma_used) { PyErr_SetString(PyExc_RuntimeError, @@ -2589,7 +2816,7 @@ dictview_new(PyObject *dict, PyTypeObjec PyErr_BadInternalCall(); return NULL; } - if (!PyDict_Check(dict)) { + if (!PyFrozenDict_Check(dict)) { /* XXX Get rid of this restriction later */ PyErr_Format(PyExc_TypeError, "%s() requires a dict argument, not '%s'", diff --git a/Objects/object.c b/Objects/object.c --- a/Objects/object.c +++ b/Objects/object.c @@ -1620,6 +1620,9 @@ _Py_ReadyTypes(void) if (PyType_Ready(&PyRange_Type) < 0) Py_FatalError("Can't initialize range type"); + if (PyType_Ready(&PyFrozenDict_Type) < 0) + Py_FatalError("Can't initialize frozendict type"); + if (PyType_Ready(&PyDict_Type) < 0) Py_FatalError("Can't initialize dict type"); diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2396,6 +2396,7 @@ _PyBuiltin_Init(void) SETBUILTIN("enumerate", &PyEnum_Type); SETBUILTIN("filter", &PyFilter_Type); SETBUILTIN("float", &PyFloat_Type); + SETBUILTIN("frozendict", &PyFrozenDict_Type); SETBUILTIN("frozenset", &PyFrozenSet_Type); SETBUILTIN("property", &PyProperty_Type); SETBUILTIN("int", &PyLong_Type);
_______________________________________________ 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