From f83be4e3f353c4cfb53a22793dd1394797988c30 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Wed, 28 Nov 2007 09:44:38 +0000 Subject: [PATCH] Added view and iterator types to collections / _abcoll I've also renamed several of the iterators to keep a consistent naming schema. --- Lib/_abcoll.py | 46 ++++++++++++++++++++++++++++++++++++++--- Objects/dictobject.c | 6 +++--- Objects/iterobject.c | 2 +- Objects/listobject.c | 4 ++-- Objects/rangeobject.c | 2 +- Objects/setobject.c | 2 +- Objects/unicodeobject.c | 2 +- 7 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py index 6363de9bb7a..d91c0107afc 100644 --- a/Lib/_abcoll.py +++ b/Lib/_abcoll.py @@ -17,8 +17,35 @@ __all__ = ["Hashable", "Iterable", "Iterator", "MappingView", "KeysView", "ItemsView", "ValuesView", "Sequence", "MutableSequence", "ByteString", + "bytearray_iterator", "bytes_iterator", "dict_itemiterator", + "dict_items", "dict_keyiterator", "dict_keys", + "dict_valueiterator", "dict_values", "list_iterator", + "list_reverseiterator", "range_iterator", "set_iterator", + "str_iterator", "tuple_iterator", "zip_iterator", ] + +### collection related types which are not exposed through builtin ### +## iterators ## +bytes_iterator = type(iter(b'')) +bytearray_iterator = type(iter(bytearray())) +#callable_iterator = ??? +dict_keyiterator = type(iter({}.keys())) +dict_valueiterator = type(iter({}.values())) +dict_itemiterator = type(iter({}.items())) +list_iterator = type(iter([])) +list_reverseiterator = type(iter(reversed([]))) +range_iterator = type(iter(range(0))) +set_iterator = type(iter(set())) +str_iterator = type(iter("")) +tuple_iterator = type(iter(())) +zip_iterator = type(iter(zip())) +## views ## +dict_keys = type({}.keys()) +dict_values = type({}.values()) +dict_items = type({}.items()) + + ### ONE-TRICK PONIES ### class Hashable(metaclass=ABCMeta): @@ -69,6 +96,19 @@ class Iterator(metaclass=ABCMeta): return True return NotImplemented +Iterator.register(bytes_iterator) +Iterator.register(bytearray_iterator) +#Iterator.register(callable_iterator) +Iterator.register(dict_keyiterator) +Iterator.register(dict_valueiterator) +Iterator.register(dict_itemiterator) +Iterator.register(list_iterator) +Iterator.register(list_reverseiterator) +Iterator.register(range_iterator) +Iterator.register(set_iterator) +Iterator.register(str_iterator) +Iterator.register(tuple_iterator) +Iterator.register(zip_iterator) class Sized(metaclass=ABCMeta): @@ -349,7 +389,7 @@ class KeysView(MappingView, Set): for key in self._mapping: yield key -KeysView.register(type({}.keys())) +KeysView.register(dict_keys) class ItemsView(MappingView, Set): @@ -367,7 +407,7 @@ class ItemsView(MappingView, Set): for key in self._mapping: yield (key, self._mapping[key]) -ItemsView.register(type({}.items())) +ItemsView.register(dict_items) class ValuesView(MappingView): @@ -382,7 +422,7 @@ class ValuesView(MappingView): for key in self._mapping: yield self._mapping[key] -ValuesView.register(type({}.values())) +ValuesView.register(dict_values) class MutableMapping(Mapping): diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 7bff7d8153e..b849f574434 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2148,7 +2148,7 @@ fail: PyTypeObject PyDictIterKey_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "dictionary-keyiterator", /* tp_name */ + "dict_keyiterator", /* tp_name */ sizeof(dictiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ @@ -2220,7 +2220,7 @@ fail: PyTypeObject PyDictIterValue_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "dictionary-valueiterator", /* tp_name */ + "dict_valueiterator", /* tp_name */ sizeof(dictiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ @@ -2306,7 +2306,7 @@ fail: PyTypeObject PyDictIterItem_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "dictionary-itemiterator", /* tp_name */ + "dict_itemiterator", /* tp_name */ sizeof(dictiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ diff --git a/Objects/iterobject.c b/Objects/iterobject.c index 32b9f949ad0..ce9c661c4cd 100644 --- a/Objects/iterobject.c +++ b/Objects/iterobject.c @@ -369,7 +369,7 @@ zipiter_next(zipiterobject *zit) static PyTypeObject PyZipIter_Type = { PyVarObject_HEAD_INIT(0, 0) - "zipiterator", /* tp_name */ + "zip_iterator", /* tp_name */ sizeof(zipiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ diff --git a/Objects/listobject.c b/Objects/listobject.c index 5df40fa9ee5..1d82e354104 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2726,7 +2726,7 @@ static PyMethodDef listiter_methods[] = { PyTypeObject PyListIter_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "listiterator", /* tp_name */ + "list_iterator", /* tp_name */ sizeof(listiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ @@ -2848,7 +2848,7 @@ static PySequenceMethods listreviter_as_sequence = { PyTypeObject PyListRevIter_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "listreverseiterator", /* tp_name */ + "list_reverseiterator", /* tp_name */ sizeof(listreviterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 0bb3e7b0ae5..d1c959a1bf5 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -369,7 +369,7 @@ static PyMethodDef rangeiter_methods[] = { PyTypeObject Pyrangeiter_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "rangeiterator", /* tp_name */ + "range_iterator", /* tp_name */ sizeof(rangeiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ diff --git a/Objects/setobject.c b/Objects/setobject.c index d85a28d386b..e4e7780e8de 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -851,7 +851,7 @@ fail: static PyTypeObject PySetIter_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "setiterator", /* tp_name */ + "set_iterator", /* tp_name */ sizeof(setiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 205576fef4e..26349ef11de 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9263,7 +9263,7 @@ static PyMethodDef unicodeiter_methods[] = { PyTypeObject PyUnicodeIter_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - "unicode_iterator", /* tp_name */ + "str_iterator", /* tp_name */ sizeof(unicodeiterobject), /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */