cpython: 88ab046fdd8a (original) (raw)

--- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -592,23 +592,24 @@ class MutableMapping(Mapping): If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v '''

-

--- a/Lib/collections/init.py +++ b/Lib/collections/init.py @@ -55,12 +55,16 @@ class OrderedDict(dict): # Individual links are kept alive by the hard reference in self.__map. # Those hard references disappear when a key is deleted from an OrderedDict.

'''

@@ -479,7 +483,7 @@ class Counter(dict): # http://code.activestate.com/recipes/259174/[](#l2.22) # Knuth, TAOCP Vol. II section 4.6.3

@@ -490,8 +494,14 @@ class Counter(dict): >>> c = Counter(a=4, b=2) # a new counter from keyword args '''

def missing(self, key): 'The count of elements not in the Counter is zero.' @@ -542,7 +552,7 @@ class Counter(dict): raise NotImplementedError( 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')

Source can be an iterable, a dictionary, or another Counter instance. @@ -562,6 +572,13 @@ class Counter(dict): # contexts. Instead, we implement straight-addition. Both the inputs # and outputs are allowed to contain zero and negative counts.

@@ -569,13 +586,13 @@ class Counter(dict): for elem, count in iterable.items(): self[elem] = count + self_get(elem, 0) else:

@@ -591,6 +608,13 @@ class Counter(dict): -1 '''

@@ -898,7 +922,14 @@ class ChainMap(MutableMapping): class UserDict(MutableMapping): # Start by filling-out the abstract methods

--- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1137,6 +1137,28 @@ class TestCounter(unittest.TestCase): self.assertEqual(c.setdefault('e', 5), 5) self.assertEqual(c['e'], 5)

+

+ def test_copying(self): # Check that counters are copyable, deepcopyable, picklable, and #have a repr/eval round-trip @@ -1258,6 +1280,16 @@ class TestCounter(unittest.TestCase): c.subtract('aaaabbcce') self.assertEqual(c, Counter(a=-1, b=0, c=-1, d=1, e=-1))

+ def test_unary(self): c = Counter(a=-5, b=0, c=5, d=10, e=15,g=40) self.assertEqual(dict(+c), dict(c=5, d=10, e=15, g=40)) @@ -1308,8 +1340,11 @@ class TestOrderedDict(unittest.TestCase) c=3, e=5).items()), pairs) # mixed input # make sure no positional args conflict with possible kwdargs

# Make sure that direct calls to init do not clear previous contents d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)]) @@ -1354,6 +1389,10 @@ class TestOrderedDict(unittest.TestCase) self.assertEqual(list(d.items()), [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g', 7)])

+ def test_abc(self): self.assertIsInstance(OrderedDict(), MutableMapping) self.assertTrue(issubclass(OrderedDict, MutableMapping)) @@ -1600,6 +1639,24 @@ class SubclassMappingTests(mapping_tests d = self._empty_mapping() self.assertRaises(KeyError, d.popitem) +class TestUserDict(unittest.TestCase): +

+

+ ################################################################################

Run tests

@@ -1611,7 +1668,8 @@ def test_main(verbose=None): NamedTupleDocs = doctest.DocTestSuite(module=collections) test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, TestCollectionABCs, TestCounter, TestChainMap,

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -191,6 +191,9 @@ Core and Builtins Library ------- +- Issue #22609: Constructors and update methods of mapping classes in the