[Python-checkins] cpython (merge 3.1 -> 3.2): Fix minor subclassing issue with collections.Counter

raymond.hettinger python-checkins at python.org
Fri Apr 15 22:23:21 CEST 2011


http://hg.python.org/cpython/rev/bb13260d56c6
changeset: 69380:bb13260d56c6
branch: 3.2
parent: 69375:850a659d9e6f
parent: 69379:a164f67d27be
user: Raymond Hettinger <python at rcn.com>
date: Fri Apr 15 13:21:30 2011 -0700
summary:
 Fix minor subclassing issue with collections.Counter
files:
 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
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -536,8 +536,8 @@
 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
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -812,6 +812,15 @@
 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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,8 @@
 - 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.
 
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /