diff -r 26c73ee77fbb Lib/collections/__init__.py --- a/Lib/collections/__init__.py Mon Sep 26 14:08:47 2016 +0200 +++ b/Lib/collections/__init__.py Mon Sep 26 21:17:15 2016 +0300 @@ -1018,6 +1018,14 @@ d[key] = value return d + def __reduce__(self): + reduce_value = super().__reduce__() + # Copy the normal dict + inst_dict = reduce_value[2].copy() + # Copy the data inside + inst_dict["data"] = inst_dict["data"].copy() + return reduce_value[:2] + (inst_dict,) + reduce_value[3:] + ################################################################################ @@ -1090,6 +1098,13 @@ self.data.extend(other.data) else: self.data.extend(other) + def __reduce__(self): + reduce_value = super().__reduce__() + # Copy the normal dict + inst_dict = reduce_value[2].copy() + # Copy the data inside + inst_dict["data"] = inst_dict["data"][:] + return reduce_value[:2] + (inst_dict,) + reduce_value[3:] diff -r 26c73ee77fbb Lib/test/test_collections.py --- a/Lib/test/test_collections.py Mon Sep 26 14:08:47 2016 +0200 +++ b/Lib/test/test_collections.py Mon Sep 26 21:17:15 2016 +0300 @@ -38,6 +38,20 @@ b=b.__name__, ), ) + + def _copy_test(self, obj): + # Test internal copy + obj_copy = obj.copy() + self.assertIsNot(obj.data, obj_copy.data) + self.assertEqual(obj.data, obj_copy.data) + + # Test copy.copy + obj.test = [1234] # Make sure instance vars are also copied. + obj_copy = copy.copy(obj) + self.assertIsNot(obj.data, obj_copy.data) + self.assertEqual(obj.data, obj_copy.data) + self.assertIs(obj.test, obj_copy.test) + def test_str_protocol(self): self._superset_test(UserString, str) @@ -47,6 +61,16 @@ def test_dict_protocol(self): self._superset_test(UserDict, dict) + def test_list_copy(self): + obj = UserList() + obj.append(123) + self._copy_test(obj) + + def test_dict_copy(self): + obj = UserDict() + obj[123] = "abc" + self._copy_test(obj) + ################################################################################ ### ChainMap (helper class for configparser and the string module)