summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-05-02 18:02:13 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-05-02 18:02:13 (GMT)
commita5ac2ce9825b7cf8b92a352dc614e0d8a4c41a6f (patch)
tree6d683027459286f155900280ec29a37aaaefec08
parent81b9656989e2acce6209184826ca135a712e3f8b (diff)
downloadcpython-a5ac2ce9825b7cf8b92a352dc614e0d8a4c41a6f.zip
cpython-a5ac2ce9825b7cf8b92a352dc614e0d8a4c41a6f.tar.gz
cpython-a5ac2ce9825b7cf8b92a352dc614e0d8a4c41a6f.tar.bz2
Backport 3.3 fixes and cleans ups.
-rw-r--r--Lib/collections.py3
-rw-r--r--Lib/test/test_collections.py30
2 files changed, 22 insertions, 11 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 3693926..b904980 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -716,6 +716,9 @@ class _ChainMap(MutableMapping):
def __contains__(self, key):
return any(key in m for m in self.maps)
+ def __bool__(self):
+ return any(self.maps)
+
@_recursive_repr()
def __repr__(self):
return '{0.__class__.__name__}({1})'.format(
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 9f036e5..2b7b68b 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -23,10 +23,12 @@ from collections import ByteString
### _ChainMap (helper class for configparser)
################################################################################
+ChainMap = _ChainMap # rename to keep test code in sync with 3.3 version
+
class TestChainMap(unittest.TestCase):
def test_basics(self):
- c = _ChainMap()
+ c = ChainMap()
c['a'] = 1
c['b'] = 2
d = c.new_child()
@@ -71,19 +73,25 @@ class TestChainMap(unittest.TestCase):
for m1, m2 in zip(d.maps, e.maps):
self.assertIsNot(m1, m2, e)
- d = d.new_child()
- d['b'] = 5
- self.assertEqual(d.maps, [{'b': 5}, {'c':30}, {'a':1, 'b':2}])
- self.assertEqual(d.parents.maps, [{'c':30}, {'a':1, 'b':2}]) # check parents
- self.assertEqual(d['b'], 5) # find first in chain
- self.assertEqual(d.parents['b'], 2) # look beyond maps[0]
+ f = d.new_child()
+ f['b'] = 5
+ self.assertEqual(f.maps, [{'b': 5}, {'c':30}, {'a':1, 'b':2}])
+ self.assertEqual(f.parents.maps, [{'c':30}, {'a':1, 'b':2}]) # check parents
+ self.assertEqual(f['b'], 5) # find first in chain
+ self.assertEqual(f.parents['b'], 2) # look beyond maps[0]
def test_contructor(self):
- self.assertEqual(_ChainMap().maps, [{}]) # no-args --> one new dict
- self.assertEqual(_ChainMap({1:2}).maps, [{1:2}]) # 1 arg --> list
+ self.assertEqual(ChainMap().maps, [{}]) # no-args --> one new dict
+ self.assertEqual(ChainMap({1:2}).maps, [{1:2}]) # 1 arg --> list
+
+ def test_bool(self):
+ self.assertFalse(ChainMap())
+ self.assertFalse(ChainMap({}, {}))
+ self.assertTrue(ChainMap({1:2}, {}))
+ self.assertTrue(ChainMap({}, {1:2}))
def test_missing(self):
- class DefaultChainMap(_ChainMap):
+ class DefaultChainMap(ChainMap):
def __missing__(self, key):
return 999
d = DefaultChainMap(dict(a=1, b=2), dict(b=20, c=30))
@@ -100,7 +108,7 @@ class TestChainMap(unittest.TestCase):
d.popitem()
def test_dict_coercion(self):
- d = _ChainMap(dict(a=1, b=2), dict(b=20, c=30))
+ d = ChainMap(dict(a=1, b=2), dict(b=20, c=30))
self.assertEqual(dict(d), dict(a=1, b=2, c=30))
self.assertEqual(dict(d.items()), dict(a=1, b=2, c=30))