From: Raymond Hettinger Date: Fri, 15 Apr 2011 20:12:21 +0000 (-0700) Subject: Fix minor subclassing issue with collections.Counter X-Git-Tag: v2.7.2rc1~148 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=37c0fe56b9053f5f388ba6d6217506a6e73bb819;p=python Fix minor subclassing issue with collections.Counter --- diff --git a/Lib/collections.py b/Lib/collections.py index bb6b15dbcc..cb720ac9ab 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -516,8 +516,8 @@ class Counter(dict): self.subtract(kwds) def copy(self): - 'Like dict.copy() but returns a Counter instance instead of a dict.' - return Counter(self) + 'Return a shallow copy.' + return self.__class__(self) def __reduce__(self): return self.__class__, (dict(self),) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index f6a43fcb1f..8bdeb3d8f1 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -689,6 +689,15 @@ class TestCounter(unittest.TestCase): self.assertEqual(len(dup), len(words)) self.assertEqual(type(dup), type(words)) + def test_copy_subclass(self): + class MyCounter(Counter): + pass + c = MyCounter('slartibartfast') + d = c.copy() + self.assertEqual(d, c) + self.assertEqual(len(d), len(c)) + self.assertEqual(type(d), type(c)) + def test_conversions(self): # Convert to: set, list, dict s = 'she sells sea shells by the sea shore' diff --git a/Misc/NEWS b/Misc/NEWS index 3f75b1e9a3..ed28d31e51 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -58,6 +58,8 @@ Library - Issue #11467: Fix urlparse behavior when handling urls which contains scheme specific part only digits. Patch by Santoso Wijaya. +- collections.Counter().copy() now works correctly for subclasses. + - Issue #11474: Fix the bug with url2pathname() handling of '/C|/' on Windows. Patch by Santoso Wijaya.