Skip to content

fix for pickling ObjectId - #28

Closed
shearic wants to merge 2 commits into
mongodb:masterfrom
shearic:master
Closed

shearic wants to merge 2 commits into
mongodb:masterfrom
shearic:master

Conversation

@shearic

@shearic shearic commented Apr 1, 2011

Copy link
Copy Markdown
Contributor

fix pickling of ObjectId by defining __getstate__() and __setstate__() because __slots__() is defined

ran into this with pymongo 1.10 with exception:
TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled

Comment thread bson/objectid.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should just return self.__id here. There doesn't seem to be any reason to hex encode it first. Without __slots__ pickle would just be serializing __dict__ which wouldn't have a hex encoded version of __id.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that makes sense. I was mimicking other cases where I've gone back and forth from ObjectId to str (such as URL parameter).

also added unit test for pickling/unpickling ObjectId
@shearic

shearic commented Apr 2, 2011

Copy link
Copy Markdown
Contributor Author

also added a unit test to make sure pickle/unpickle yields original

Comment thread bson/objectid.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you had the right idea here before. Calling validate seems like the correct thing to do.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, never mind. It certainly wasn't being called before and just adds overhead. This is fine now. Thanks for the patch!

@behackett

Copy link
Copy Markdown
Member

Thanks for the patch!

@behackett behackett closed this Apr 2, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants