Преглед на файлове

Fix some more Python 3 compat issues

Signed-off-by: Dan O'Reilly <oreilldf@gmail.com>
Dan O'Reilly преди 10 години
родител
ревизия
3d5aa6aef9
променени са 3 файла, в които са добавени 25 реда и са изтрити 14 реда
  1. 15 10
      python/google/protobuf/internal/message_test.py
  2. 1 1
      python/google/protobuf/internal/text_format_test.py
  3. 9 3
      python/google/protobuf/text_format.py

+ 15 - 10
python/google/protobuf/internal/message_test.py

@@ -49,6 +49,11 @@ import operator
 import pickle
 import pickle
 import sys
 import sys
 
 
+import six
+
+if six.PY3:
+  long = int
+
 import unittest
 import unittest
 from google.protobuf.internal import _parameterized
 from google.protobuf.internal import _parameterized
 from google.protobuf import map_unittest_pb2
 from google.protobuf import map_unittest_pb2
@@ -675,7 +680,7 @@ class MessageTest(unittest.TestCase):
     in the value being converted to a Unicode string."""
     in the value being converted to a Unicode string."""
     m = message_module.TestAllTypes()
     m = message_module.TestAllTypes()
     m.optional_string = str('')
     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?
 # 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('abc' in msg.map_string_string)
     self.assertTrue(888 in msg.map_int32_enum)
     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
     # Accessing an unset key still throws TypeError of the type of the key
     # is incorrect.
     # is incorrect.
@@ -1311,13 +1316,13 @@ class Proto3Test(unittest.TestCase):
 
 
     msg.map_string_string[bytes_obj] = bytes_obj
     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(key, unicode_obj)
     self.assertEqual(value, 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):
   def testMessageMap(self):
     msg = map_unittest_pb2.TestMap()
     msg = map_unittest_pb2.TestMap()
@@ -1502,7 +1507,7 @@ class Proto3Test(unittest.TestCase):
   def testMapIteration(self):
   def testMapIteration(self):
     msg = map_unittest_pb2.TestMap()
     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.
       # Should not be reached.
       self.assertTrue(False)
       self.assertTrue(False)
 
 
@@ -1512,7 +1517,7 @@ class Proto3Test(unittest.TestCase):
     self.assertEqual(3, len(msg.map_int32_int32))
     self.assertEqual(3, len(msg.map_int32_int32))
 
 
     matching_dict = {2: 4, 3: 6, 4: 8}
     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):
   def testMapIterationClearMessage(self):
     # Iterator needs to work even if message and map are deleted.
     # 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[3] = 6
     msg.map_int32_int32[4] = 8
     msg.map_int32_int32[4] = 8
 
 
-    it = msg.map_int32_int32.iteritems()
+    it = msg.map_int32_int32.items()
     del msg
     del msg
 
 
     matching_dict = {2: 4, 3: 6, 4: 8}
     matching_dict = {2: 4, 3: 6, 4: 8}
@@ -1550,7 +1555,7 @@ class Proto3Test(unittest.TestCase):
 
 
     msg.ClearField('map_int32_int32')
     msg.ClearField('map_int32_int32')
     matching_dict = {2: 4, 3: 6, 4: 8}
     matching_dict = {2: 4, 3: 6, 4: 8}
-    self.assertMapIterEquals(map.iteritems(), matching_dict)
+    self.assertMapIterEquals(map.items(), matching_dict)
 
 
   def testMapIterValidAfterFieldCleared(self):
   def testMapIterValidAfterFieldCleared(self):
     # Map iterator needs to work even if field is cleared.
     # 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[3] = 6
     msg.map_int32_int32[4] = 8
     msg.map_int32_int32[4] = 8
 
 
-    it = msg.map_int32_int32.iteritems()
+    it = msg.map_int32_int32.items()
 
 
     msg.ClearField('map_int32_int32')
     msg.ClearField('map_int32_int32')
     matching_dict = {2: 4, 3: 6, 4: 8}
     matching_dict = {2: 4, 3: 6, 4: 8}

+ 1 - 1
python/google/protobuf/internal/text_format_test.py

@@ -101,7 +101,7 @@ class TextFormatTest(TextFormatBase):
         'repeated_string: "\\303\\274\\352\\234\\237"\n')
         'repeated_string: "\\303\\274\\352\\234\\237"\n')
 
 
   def testPrintExoticUnicodeSubclass(self, message_module):
   def testPrintExoticUnicodeSubclass(self, message_module):
-    class UnicodeSub(unicode):
+    class UnicodeSub(six.text_type):
       pass
       pass
     message = message_module.TestAllTypes()
     message = message_module.TestAllTypes()
     message.repeated_string.append(UnicodeSub(u'\u00fc\ua71f'))
     message.repeated_string.append(UnicodeSub(u'\u00fc\ua71f'))

+ 9 - 3
python/google/protobuf/text_format.py

@@ -92,7 +92,10 @@ def MessageToString(message, as_utf8=False, as_one_line=False,
   Returns:
   Returns:
     A string of the text formatted protocol buffer message.
     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,
   PrintMessage(message, out, as_utf8=as_utf8, as_one_line=as_one_line,
                pointy_brackets=pointy_brackets,
                pointy_brackets=pointy_brackets,
                use_index_order=use_index_order,
                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,
                  use_index_order=use_index_order,
                  float_format=float_format)
                  float_format=float_format)
 
 
-
 def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False,
 def PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False,
                pointy_brackets=False, use_index_order=False, float_format=None):
                pointy_brackets=False, use_index_order=False, float_format=None):
   """Print a single field name/value pair.  For repeated fields, the value
   """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.
     # For groups, use the capitalized name.
     out.write(field.message_type.name)
     out.write(field.message_type.name)
   else:
   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:
   if field.cpp_type != descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
     # The colon is optional in this case, but our cross-language golden files
     # The colon is optional in this case, but our cross-language golden files