From 0abfa25254a18d275775ab6a1274fe177a7de990 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 24 Sep 2014 17:18:35 -0400 Subject: [PATCH 1/2] Fix #132: enforce 64-bit ranges on long values for protobuf attrs. --- gcloud/datastore/helpers.py | 5 ++++- gcloud/datastore/test_helpers.py | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index bde6cbafac66..1ed40878acd5 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -2,11 +2,14 @@ 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. @@ -53,7 +56,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 diff --git a/gcloud/datastore/test_helpers.py b/gcloud/datastore/test_helpers.py index d00e1efea7a8..2606012779a8 100644 --- a/gcloud/datastore/test_helpers.py +++ b/gcloud/datastore/test_helpers.py @@ -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') From 68c0505d5f24b3468f3754d7e26aa1a75d997e99 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 25 Sep 2014 14:17:34 -0400 Subject: [PATCH 2/2] Incorporate feedback from @dhermes. --- gcloud/datastore/helpers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py index 1ed40878acd5..3eb1356d4d9d 100644 --- a/gcloud/datastore/helpers.py +++ b/gcloud/datastore/helpers.py @@ -10,6 +10,7 @@ INT64 = Int64ValueChecker().CheckValue + def get_protobuf_attribute_and_value(val): """Given a value, return the protobuf attribute name and proper value.