Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created Mar 23, 2011
Embed
What would you like to do?
Minimal example that causes an error when deep copying a mongoengine Document.
from copy import deepcopy
from mongoengine import Document, ReferenceField, connect
class Foo(Document):
pass
class Bar(Document):
foo = ReferenceField(Foo)
connect('tmp-deepcopy')
Foo.drop_collection()
foo = Foo()
foo.save()
Bar.drop_collection()
bar = Bar(foo=foo)
bar.save()
deepcopy(bar) # succeeds
bar = Bar.objects.get(id=bar.id)
deepcopy(bar) # fails
Traceback (most recent call last):
File "deepcopy.py", line 22, in <module>
deepcopy(Bar.objects.get(id=bar.id))
File "/usr/lib/python2.6/copy.py", line 189, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib/python2.6/copy.py", line 338, in _reconstruct
state = deepcopy(state, memo)
File "/usr/lib/python2.6/copy.py", line 162, in deepcopy
y = copier(x, memo)
File "/usr/lib/python2.6/copy.py", line 255, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python2.6/copy.py", line 162, in deepcopy
y = copier(x, memo)
File "/usr/lib/python2.6/copy.py", line 255, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/usr/lib/python2.6/copy.py", line 171, in deepcopy
copier = getattr(x, "__deepcopy__", None)
File "build/bdist.linux-i686/egg/bson/dbref.py", line 81, in __getattr__
KeyError: '__deepcopy__'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment