Issue #24286: Register dict views with the MappingView ABCs.

This commit is contained in:
Raymond Hettinger 2015-05-26 01:35:54 -07:00
parent 902176be89
commit 1a7c3571c7
4 changed files with 31 additions and 0 deletions

View File

@ -453,6 +453,7 @@ class KeysView(MappingView, Set):
for key in self._mapping:
yield key
KeysView.register(type({}.viewkeys()))
class ItemsView(MappingView, Set):
@ -473,6 +474,7 @@ class ItemsView(MappingView, Set):
for key in self._mapping:
yield (key, self._mapping[key])
ItemsView.register(type({}.viewitems()))
class ValuesView(MappingView):
@ -486,6 +488,7 @@ class ValuesView(MappingView):
for key in self._mapping:
yield self._mapping[key]
ValuesView.register(type({}.viewvalues()))
class MutableMapping(Mapping):

View File

@ -1286,6 +1286,10 @@ class TestOrderedDict(unittest.TestCase):
self.assertEqual(list(od.viewvalues()), [None for k in s])
self.assertEqual(list(od.viewitems()), [(k, None) for k in s])
# See http://bugs.python.org/issue24286
self.assertEqual(od.viewkeys(), dict(od).viewkeys())
self.assertEqual(od.viewitems(), dict(od).viewitems())
def test_override_update(self):
# Verify that subclasses can override update() without breaking __init__()
class MyOD(OrderedDict):

View File

@ -1,4 +1,5 @@
import unittest
import collections
from test import test_support
class DictSetTest(unittest.TestCase):
@ -164,7 +165,26 @@ class DictSetTest(unittest.TestCase):
d[42] = d.viewvalues()
self.assertRaises(RuntimeError, repr, d)
def test_abc_registry(self):
d = dict(a=1)
self.assertIsInstance(d.viewkeys(), collections.KeysView)
self.assertIsInstance(d.viewkeys(), collections.MappingView)
self.assertIsInstance(d.viewkeys(), collections.Set)
self.assertIsInstance(d.viewkeys(), collections.Sized)
self.assertIsInstance(d.viewkeys(), collections.Iterable)
self.assertIsInstance(d.viewkeys(), collections.Container)
self.assertIsInstance(d.viewvalues(), collections.ValuesView)
self.assertIsInstance(d.viewvalues(), collections.MappingView)
self.assertIsInstance(d.viewvalues(), collections.Sized)
self.assertIsInstance(d.viewitems(), collections.ItemsView)
self.assertIsInstance(d.viewitems(), collections.MappingView)
self.assertIsInstance(d.viewitems(), collections.Set)
self.assertIsInstance(d.viewitems(), collections.Sized)
self.assertIsInstance(d.viewitems(), collections.Iterable)
self.assertIsInstance(d.viewitems(), collections.Container)
def test_main():

View File

@ -21,6 +21,10 @@ Library
- Issue #24257: Fixed segmentation fault in sqlite3.Row constructor with faked
cursor type.
- Issue #24286: Dict view were not registered with the MappingView abstract
base classes. This caused key and item views in OrderedDict to not be equal
to their regular dict counterparts.
- Issue #22107: tempfile.gettempdir() and tempfile.mkdtemp() now try again
when a directory with the chosen name already exists on Windows as well as
on Unix. tempfile.mkstemp() now fails early if parent directory is not