Skip to content

Commit

Permalink
Merge pull request #182 from tseaver/132-enforce_int64_bounds_for_pro…
Browse files Browse the repository at this point in the history
…tobuf_values

Fix #132:  enforce 64-bit ranges on long values for protobuf attrs.
  • Loading branch information
dhermes committed Sep 25, 2014
2 parents b97ce73 + 68c0505 commit f5ec306
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
import calendar
from datetime import datetime, timedelta

from google.protobuf.internal.type_checkers import Int64ValueChecker
import pytz

from gcloud.datastore.key import Key


INT64 = Int64ValueChecker().CheckValue


def get_protobuf_attribute_and_value(val):
"""Given a value, return the protobuf attribute name and proper value.
Expand Down Expand Up @@ -53,7 +57,7 @@ def get_protobuf_attribute_and_value(val):
elif isinstance(val, float):
name, value = 'double', val
elif isinstance(val, (int, long)):
name, value = 'integer', val
name, value = 'integer', INT64(val)
elif isinstance(val, basestring):
name, value = 'string', val

Expand Down
10 changes: 9 additions & 1 deletion gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,19 @@ def test_int(self):
self.assertEqual(value, 42)

def test_long(self):
must_be_long = 1 << 63
must_be_long = (1 << 63) - 1
name, value = self._callFUT(must_be_long)
self.assertEqual(name, 'integer_value')
self.assertEqual(value, must_be_long)

def test_long_too_small(self):
too_small = -(1 << 63) - 1
self.assertRaises(ValueError, self._callFUT, too_small)

def test_long_too_large(self):
too_large = 1 << 63
self.assertRaises(ValueError, self._callFUT, too_large)

def test_native_str(self):
name, value = self._callFUT('str')
self.assertEqual(name, 'string_value')
Expand Down

0 comments on commit f5ec306

Please sign in to comment.