From 3d5aa6aef97f7ba9394f226778fdba91a9f89d59 Mon Sep 17 00:00:00 2001 From: Dan O'Reilly Date: Fri, 14 Aug 2015 16:12:34 -0400 Subject: Fix some more Python 3 compat issues Signed-off-by: Dan O'Reilly --- python/google/protobuf/internal/message_test.py | 25 +++++++++++++--------- .../google/protobuf/internal/text_format_test.py | 2 +- python/google/protobuf/text_format.py | 12 ++++++++--- 3 files changed, 25 insertions(+), 14 deletions(-) (limited to 'python') diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py index 4dc92752..66356c92 100755 --- a/python/google/protobuf/internal/message_test.py +++ b/python/google/protobuf/internal/message_test.py @@ -49,6 +49,11 @@ import operator import pickle import sys +import six + +if six.PY3: + long = int + import unittest from google.protobuf.internal import _parameterized from google.protobuf import map_unittest_pb2 @@ -675,7 +680,7 @@ class MessageTest(unittest.TestCase): in the value being converted to a Unicode string.""" m = message_module.TestAllTypes() m.optional_string = str('') - self.assertTrue(isinstance(m.optional_string, unicode)) + self.assertTrue(isinstance(m.optional_string, six.text_type)) # TODO(haberman): why are these tests Google-internal only? @@ -1228,7 +1233,7 @@ class Proto3Test(unittest.TestCase): self.assertTrue('abc' in msg.map_string_string) self.assertTrue(888 in msg.map_int32_enum) - self.assertTrue(isinstance(msg.map_string_string['abc'], unicode)) + self.assertTrue(isinstance(msg.map_string_string['abc'], six.text_type)) # Accessing an unset key still throws TypeError of the type of the key # is incorrect. @@ -1311,13 +1316,13 @@ class Proto3Test(unittest.TestCase): msg.map_string_string[bytes_obj] = bytes_obj - (key, value) = msg.map_string_string.items()[0] + (key, value) = list(msg.map_string_string.items())[0] self.assertEqual(key, unicode_obj) self.assertEqual(value, unicode_obj) - self.assertTrue(isinstance(key, unicode)) - self.assertTrue(isinstance(value, unicode)) + self.assertTrue(isinstance(key, six.text_type)) + self.assertTrue(isinstance(value, six.text_type)) def testMessageMap(self): msg = map_unittest_pb2.TestMap() @@ -1502,7 +1507,7 @@ class Proto3Test(unittest.TestCase): def testMapIteration(self): msg = map_unittest_pb2.TestMap() - for k, v in msg.map_int32_int32.iteritems(): + for k, v in msg.map_int32_int32.items(): # Should not be reached. self.assertTrue(False) @@ -1512,7 +1517,7 @@ class Proto3Test(unittest.TestCase): self.assertEqual(3, len(msg.map_int32_int32)) matching_dict = {2: 4, 3: 6, 4: 8} - self.assertMapIterEquals(msg.map_int32_int32.iteritems(), matching_dict) + self.assertMapIterEquals(msg.map_int32_int32.items(), matching_dict) def testMapIterationClearMessage(self): # Iterator needs to work even if message and map are deleted. @@ -1522,7 +1527,7 @@ class Proto3Test(unittest.TestCase): msg.map_int32_int32[3] = 6 msg.map_int32_int32[4] = 8 - it = msg.map_int32_int32.iteritems() + it = msg.map_int32_int32.items() del msg matching_dict = {2: 4, 3: 6, 4: 8} @@ -1550,7 +1555,7 @@ class Proto3Test(unittest.TestCase): msg.ClearField('map_int32_int32') matching_dict = {2: 4, 3: 6, 4: 8} - self.assertMapIterEquals(map.iteritems(), matching_dict) + self.assertMapIterEquals(map.items(), matching_dict) def testMapIterValidAfterFieldCleared(self): # Map iterator needs to work even if field is cleared. @@ -1562,7 +1567,7 @@ class Proto3Test(unittest.TestCase): msg.map_int32_int32[3] = 6 msg.map_int32_int32[4] = 8 - it = msg.map_int32_int32.iteritems() + it = msg.map_int32_int32.items() msg.ClearField('map_int32_int32') matching_dict = {2: 4, 3: 6, 4: 8} diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py index 55b32249..49e6332c 100755 --- a/python/google/protobuf/internal/text_format_test.py +++ b/python/google/protobuf/internal/text_format_test.py @@ -101,7 +101,7 @@ class TextFormatTest(TextFormatBase): 'repeated_string: "\\303\\274\\352\\234\\237"\n') def testPrintExoticUnicodeSubclass(self, message_module): - class UnicodeSub(unicode): + class UnicodeSub(six.text_type): pass message = message_module.TestAllTypes() message.repeated_string.append(UnicodeSub(u'\u00fc\ua71f')) diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py index d4c4610f..5e4d10b1 100755 --- a/python/google/protobuf/text_format.py +++ b/python/google/protobuf/text_format.py @@ -92,7 +92,10 @@ def MessageToString(message, as_utf8=False, as_one_line=False, Returns: A string of the text formatted protocol buffer message. """ - out = io.BytesIO() + if as_utf8: + out = io.BytesIO() + else: + out = io.BytesIO() PrintMessage(message, out, as_utf8=as_utf8, as_one_line=as_one_line, pointy_brackets=pointy_brackets, use_index_order=use_index_order, @@ -139,7 +142,6 @@ def PrintMessage(message, out, indent=0, as_utf8=False, as_one_line=False, use_index_order=use_index_order, float_format=float_format) - def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False, pointy_brackets=False, use_index_order=False, float_format=None): """Print a single field name/value pair. For repeated fields, the value @@ -160,7 +162,11 @@ def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False, # For groups, use the capitalized name. out.write(field.message_type.name) else: - out.write(field.name) + if isinstance(field.name, six.text_type): + name = field.name.encode('utf-8') + else: + name = field.name + out.write(name) if field.cpp_type != descriptor.FieldDescriptor.CPPTYPE_MESSAGE: # The colon is optional in this case, but our cross-language golden files -- cgit v1.2.3