Skip to content

Commit a455fdd

Browse files
committed
Make Response pickleable
Fixes #558 for 2.x
1 parent ca56f54 commit a455fdd

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

elasticsearch_dsl/result.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def __repr__(self):
2828
def __len__(self):
2929
return len(self.hits)
3030

31+
def __getstate__(self):
32+
return (self._d_, self._callbacks)
33+
34+
def __setstate__(self, state):
35+
super(AttrDict, self).__setattr__('_d_', state[0])
36+
super(AttrDict, self).__setattr__('_callbacks', state[1])
37+
3138
def success(self):
3239
return self._shards.total == self._shards.successful and not self.timed_out
3340

test_elasticsearch_dsl/test_result.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
import pickle
2+
13
from pytest import raises
24

35
from elasticsearch_dsl import result
46

7+
def test_response_is_pickleable(dummy_response):
8+
res = result.Response(dummy_response)
9+
res.hits
10+
r = pickle.loads(pickle.dumps(res))
11+
12+
assert r == res
13+
assert r.hits == res.hits
14+
515
def test_attribute_error_in_hits_is_not_hidden(dummy_response):
616
def f(hit):
717
raise AttributeError()

0 commit comments

Comments
 (0)