[New-bugs-announce] [issue7848] copy.copy corrupts objects that return false value from __getstate__

Albertas Agejevas report at bugs.python.org
Wed Feb 3 19:18:45 CET 2010


New submission from Albertas Agejevas <alga at pov.lt>:

When copy.copy is used on an object whose __getstate__ returns 0, it can produce a corrupt copy of an object:

>>> import copy
>>> class Foo(object):
...     def __init__(self):
...        self.value = 0
...     def __getstate__(self):
...        return self.value
...     def __setstate__(self, v):
...        self.value = v
... 
>>> one = Foo()
>>> two = copy.copy(one)
>>> two.value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'value'

Pickling/unpickling works fine for this object, so this appears to be a bug in copy._reconstruct.

This is not a contrived example, BTrees.Length.Length from ZODB uses such a __getstate__.

----------
components: Library (Lib)
messages: 98793
nosy: alga
severity: normal
status: open
title: copy.copy corrupts objects that return false value from __getstate__
type: behavior
versions: Python 2.5, Python 2.6, Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7848>
_______________________________________


More information about the New-bugs-announce mailing list