From 1c746c28f3cfe798ed4d7b51f50adaa685a4fb0e Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 15 Apr 2011 13:16:46 -0700 Subject: Fix minor subclassing issue with collections.Counter --- Lib/collections.py | 4 ++-- Lib/test/test_collections.py | 9 +++++++++ Misc/NEWS | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Lib/collections.py b/Lib/collections.py index 27bb5e1..30301ce 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -459,8 +459,8 @@ class Counter(dict): self.update(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 8989ac3..3d6ad09 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -680,6 +680,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 7ea8850..c424e1e 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. -- cgit v0.12