Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack to fix pickling #197

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 9 additions & 4 deletions rtree/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,18 @@ def __repr__(self):
self.get_size())

def __getstate__(self):
state = self.__dict__.copy()
del state["handle"]
return state
attrs = self.__dict__.copy()
del attrs["handle"]
entries = self.intersection(self.bounds, objects=True)
entries = [(item.id, item.bbox, item.object) for item in entries]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When interleaved=True, this seems to require item.bbox, but when interleaved=False, this seems to require item.bounds. I'm not even sure why there are two names for the same thing...

Copy link
Member

Choose a reason for hiding this comment

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

indeed that seems like some kind of bug due to refactorings. It should be tracked down all the way to figure out what's going on.

return attrs, entries

def __setstate__(self, state):
self.__dict__.update(state)
attrs, entries = state
self.__dict__.update(attrs)
self.handle = IndexHandle(self.properties.handle)
for item in entries:
self.insert(*item)

def dumps(self, obj):
return pickle.dumps(obj)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,19 @@ class TestPickling(unittest.TestCase):

def test_index(self):
idx = rtree.index.Index()
idx.insert(0, [0, 1, 2, 3], 4)
unpickled = pickle.loads(pickle.dumps(idx))
self.assertNotEqual(idx.handle, unpickled.handle)
self.assertEqual(idx.properties.as_dict(),
unpickled.properties.as_dict())
self.assertEqual(idx.interleaved, unpickled.interleaved)
self.assertEqual(idx.get_size(), unpickled.get_size())
self.assertEqual(idx.bounds, unpickled.bounds)
a = next(idx.intersection(idx.bounds, objects=True))
b = next(unpickled.intersection(unpickled.bounds, objects=True))
self.assertEqual(a.id, b.id)
self.assertEqual(a.bounds, b.bounds)
self.assertEqual(a.object, b.object)

def test_property(self):
p = rtree.index.Property()
Expand Down