diff options
author | Raymond Hettinger <python@rcn.com> | 2011-04-15 20:21:30 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-04-15 20:21:30 (GMT) |
commit | 6c9e5b779f4cbb16f529f0451a07eaa57caad896 (patch) | |
tree | a30993e15b87a81825f4202207b62d8ab6d9bf85 | |
parent | 8881868e08439cf758c0bcdcf7cdb54c822ae1f2 (diff) | |
parent | 1c746c28f3cfe798ed4d7b51f50adaa685a4fb0e (diff) | |
download | cpython-6c9e5b779f4cbb16f529f0451a07eaa57caad896.zip cpython-6c9e5b779f4cbb16f529f0451a07eaa57caad896.tar.gz cpython-6c9e5b779f4cbb16f529f0451a07eaa57caad896.tar.bz2 |
Fix minor subclassing issue with collections.Counter
-rw-r--r-- | Lib/collections.py | 4 | ||||
-rw-r--r-- | Lib/test/test_collections.py | 9 | ||||
-rw-r--r-- | Misc/NEWS | 2 |
3 files changed, 13 insertions, 2 deletions
diff --git a/Lib/collections.py b/Lib/collections.py index 98c4325..c77b089 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -536,8 +536,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 b3e0907..b2cad70 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -812,6 +812,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' @@ -63,6 +63,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. |