Skip to content

Commit

Permalink
Merge pull request #254 from tseaver/171-rework_query_clone_deepcopy
Browse files Browse the repository at this point in the history
Fix #171: rework query clone to avoid spurious copy of dataset
  • Loading branch information
tseaver committed Oct 17, 2014
2 parents 08deef5 + 7eae97f commit 83e8262
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 3 additions & 3 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Create / interact with gcloud datastore queries."""

import base64
import copy

from gcloud.datastore import datastore_v1_pb2 as datastore_pb
from gcloud.datastore import _helpers
Expand Down Expand Up @@ -67,8 +66,9 @@ def _clone(self):
:rtype: :class:`gcloud.datastore.query.Query`
:returns: a copy of 'self'.
"""
clone = copy.deepcopy(self)
clone._dataset = self._dataset # Shallow copy the dataset.
clone = self.__class__(dataset=self._dataset)
clone._pb.CopyFrom(self._pb)
clone._cursor = self._cursor
return clone

def to_protobuf(self):
Expand Down
3 changes: 3 additions & 0 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ def test__clone(self):

_DATASET = 'DATASET'
_KIND = 'KIND'
_CURSOR = 'DEADBEEF'
dataset = Dataset(_DATASET)
query = self._makeOne(_KIND, dataset)
query._cursor = _CURSOR
clone = query._clone()
self.assertFalse(clone is query)
self.assertTrue(isinstance(clone, self._getTargetClass()))
self.assertTrue(clone.dataset() is dataset)
kq_pb, = list(clone.kind())
self.assertEqual(kq_pb.name, _KIND)
self.assertEqual(clone._cursor, _CURSOR)

def test_to_protobuf_empty(self):
query = self._makeOne()
Expand Down

0 comments on commit 83e8262

Please sign in to comment.