summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-04-15 20:23:01 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-04-15 20:23:01 (GMT)
commitb5a40d4d3f5f472fc19ff84e745f150a21391d09 (patch)
tree1f60a5cf88ad7892866b7226e50c0d3695bf1a9e
parent8437fe2708706eb60769c5a68f3371a244123b3e (diff)
parent6c9e5b779f4cbb16f529f0451a07eaa57caad896 (diff)
downloadcpython-b5a40d4d3f5f472fc19ff84e745f150a21391d09.zip
cpython-b5a40d4d3f5f472fc19ff84e745f150a21391d09.tar.gz
cpython-b5a40d4d3f5f472fc19ff84e745f150a21391d09.tar.bz2
Fix minor subclassing issue with collections.Counter
-rw-r--r--Lib/collections/__init__.py4
-rw-r--r--Lib/test/test_collections.py9
-rw-r--r--Misc/NEWS2
3 files changed, 13 insertions, 2 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 122dfb6..bd19614 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -565,8 +565,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 d4cc4a8..f1f1094 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -870,6 +870,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 f716f2f..6d07bde 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -113,6 +113,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.