Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/_weakrefset.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def __contains__(self, item):
def __reduce__(self):
return self.__class__, (list(self),), self.__getstate__()

def __copy__(self):
raise TypeError(f"cannot copy {type(self).__name__!r} object")

def __deepcopy__(self, memo):
raise TypeError(f"cannot copy {type(self).__name__!r} object")

def add(self, item):
self.data.add(ref(item, self._remove))

Expand Down
24 changes: 6 additions & 18 deletions Lib/test/test_weakset.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,26 +457,14 @@ def test_abc(self):
def test_copying(self):
for cls in WeakSet, WeakSetWithSlots:
s = cls(self.items)
s.x = ['x']
s.z = ['z']

dup = copy.copy(s)
self.assertIsInstance(dup, cls)
self.assertEqual(dup, s)
self.assertIsNot(dup, s)
self.assertIs(dup.x, s.x)
self.assertIs(dup.z, s.z)
self.assertNotHasAttr(dup, 'y')

dup = copy.deepcopy(s)
with self.assertRaises(TypeError):
copy.copy(s)
with self.assertRaises(TypeError):
copy.deepcopy(s)
dup = s.copy()
self.assertIsInstance(dup, cls)
self.assertEqual(dup, s)
self.assertIsNot(dup, s)
self.assertEqual(dup.x, s.x)
self.assertIsNot(dup.x, s.x)
self.assertEqual(dup.z, s.z)
self.assertIsNot(dup.z, s.z)
self.assertNotHasAttr(dup, 'y')
self.assertIsNot(dup.data, s.data)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:func:`copy.copy` and :func:`copy.deepcopy` of :class:`weakref.WeakSet`
now raise :exc:`TypeError` instead of creating a broken copy
which shares internal state with the original.
Use the :meth:`!copy` method instead.
Loading