diff --git a/python/google/protobuf/internal/_parameterized.py b/python/google/protobuf/internal/_parameterized.py index 3002b23051..6c5a2bac0f 100755 --- a/python/google/protobuf/internal/_parameterized.py +++ b/python/google/protobuf/internal/_parameterized.py @@ -149,7 +149,10 @@ import collections import functools import re import types -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest import uuid import six diff --git a/python/google/protobuf/internal/descriptor_database_test.py b/python/google/protobuf/internal/descriptor_database_test.py index 3241cb7281..1baff7d1d1 100644 --- a/python/google/protobuf/internal/descriptor_database_test.py +++ b/python/google/protobuf/internal/descriptor_database_test.py @@ -34,7 +34,10 @@ __author__ = 'matthewtoia@google.com (Matt Toia)' -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf import descriptor_pb2 from google.protobuf.internal import factory_test2_pb2 from google.protobuf import descriptor_database diff --git a/python/google/protobuf/internal/descriptor_pool_test.py b/python/google/protobuf/internal/descriptor_pool_test.py index 64b5d17244..2a482fbabc 100644 --- a/python/google/protobuf/internal/descriptor_pool_test.py +++ b/python/google/protobuf/internal/descriptor_pool_test.py @@ -35,9 +35,11 @@ __author__ = 'matthewtoia@google.com (Matt Toia)' import os -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest -import unittest from google.protobuf import unittest_pb2 from google.protobuf import descriptor_pb2 from google.protobuf.internal import api_implementation diff --git a/python/google/protobuf/internal/descriptor_test.py b/python/google/protobuf/internal/descriptor_test.py index a40ec0e4d5..34843a6140 100755 --- a/python/google/protobuf/internal/descriptor_test.py +++ b/python/google/protobuf/internal/descriptor_test.py @@ -36,7 +36,6 @@ __author__ = 'robinson@google.com (Will Robinson)' import sys -import unittest from google.protobuf import unittest_custom_options_pb2 from google.protobuf import unittest_import_pb2 from google.protobuf import unittest_pb2 @@ -46,6 +45,11 @@ from google.protobuf import descriptor from google.protobuf import symbol_database from google.protobuf import text_format +try: + import unittest2 as unittest +except ImportError: + import unittest + TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII = """ name: 'TestEmptyMessage' @@ -455,7 +459,7 @@ class GeneratedDescriptorTest(unittest.TestCase): # properties of an immutable abc.Mapping. self.assertGreater(len(mapping), 0) # Sized self.assertEqual(len(mapping), len(list(mapping))) # Iterable - if sys.version_info.major >= 3: + if sys.version_info >= (3,): key, item = next(iter(mapping.items())) else: key, item = mapping.items()[0] @@ -464,7 +468,7 @@ class GeneratedDescriptorTest(unittest.TestCase): # keys(), iterkeys() &co item = (next(iter(mapping.keys())), next(iter(mapping.values()))) self.assertEqual(item, next(iter(mapping.items()))) - if sys.version_info.major < 3: + if sys.version_info < (3,): def CheckItems(seq, iterator): self.assertEqual(next(iterator), seq[0]) self.assertEqual(list(iterator), seq[1:]) diff --git a/python/google/protobuf/internal/generator_test.py b/python/google/protobuf/internal/generator_test.py index f6135fb992..bdd7382a9e 100755 --- a/python/google/protobuf/internal/generator_test.py +++ b/python/google/protobuf/internal/generator_test.py @@ -41,7 +41,10 @@ further ensures that we can use Python protocol message objects as we expect. __author__ = 'robinson@google.com (Will Robinson)' -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf.internal import test_bad_identifiers_pb2 from google.protobuf import unittest_custom_options_pb2 from google.protobuf import unittest_import_pb2 @@ -153,7 +156,7 @@ class GeneratorTest(unittest.TestCase): # extension and for its value to be set to -789. def testNestedTypes(self): - self.assertEquals( + self.assertEqual( set(unittest_pb2.TestAllTypes.DESCRIPTOR.nested_types), set([ unittest_pb2.TestAllTypes.NestedMessage.DESCRIPTOR, diff --git a/python/google/protobuf/internal/message_factory_test.py b/python/google/protobuf/internal/message_factory_test.py index 27a3f08bc8..0d880a75ea 100644 --- a/python/google/protobuf/internal/message_factory_test.py +++ b/python/google/protobuf/internal/message_factory_test.py @@ -34,7 +34,10 @@ __author__ = 'matthewtoia@google.com (Matt Toia)' -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf import descriptor_pb2 from google.protobuf.internal import factory_test1_pb2 from google.protobuf.internal import factory_test2_pb2 diff --git a/python/google/protobuf/internal/message_test.py b/python/google/protobuf/internal/message_test.py index 66356c9251..cf3fcfe917 100755 --- a/python/google/protobuf/internal/message_test.py +++ b/python/google/protobuf/internal/message_test.py @@ -54,7 +54,10 @@ import six if six.PY3: long = int -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf.internal import _parameterized from google.protobuf import map_unittest_pb2 from google.protobuf import unittest_pb2 @@ -324,7 +327,7 @@ class MessageTest(unittest.TestCase): def testHighPrecisionFloatPrinting(self, message_module): message = message_module.TestAllTypes() message.optional_double = 0.12345678912345678 - if sys.version_info.major >= 3: + if sys.version_info >= (3,): self.assertEqual(str(message), 'optional_double: 0.12345678912345678\n') else: self.assertEqual(str(message), 'optional_double: 0.123456789123\n') @@ -443,7 +446,7 @@ class MessageTest(unittest.TestCase): message.repeated_nested_message.sort(key=get_bb, reverse=True) self.assertEqual([k.bb for k in message.repeated_nested_message], [6, 5, 4, 3, 2, 1]) - if sys.version_info.major >= 3: return # No cmp sorting in PY3. + if sys.version_info >= (3,): return # No cmp sorting in PY3. message.repeated_nested_message.sort(sort_function=cmp_bb) self.assertEqual([k.bb for k in message.repeated_nested_message], [1, 2, 3, 4, 5, 6]) @@ -462,7 +465,7 @@ class MessageTest(unittest.TestCase): self.assertEqual(list(message.repeated_int32), [-1, -2, -3]) message.repeated_int32.sort(key=abs, reverse=True) self.assertEqual(list(message.repeated_int32), [-3, -2, -1]) - if sys.version_info.major < 3: # No cmp sorting in PY3. + if sys.version_info < (3,): # No cmp sorting in PY3. abs_cmp = lambda a, b: cmp(abs(a), abs(b)) message.repeated_int32.sort(sort_function=abs_cmp) self.assertEqual(list(message.repeated_int32), [-1, -2, -3]) @@ -476,7 +479,7 @@ class MessageTest(unittest.TestCase): self.assertEqual(list(message.repeated_string), ['c', 'bb', 'aaa']) message.repeated_string.sort(key=len, reverse=True) self.assertEqual(list(message.repeated_string), ['aaa', 'bb', 'c']) - if sys.version_info.major < 3: # No cmp sorting in PY3. + if sys.version_info < (3,): # No cmp sorting in PY3. len_cmp = lambda a, b: cmp(len(a), len(b)) message.repeated_string.sort(sort_function=len_cmp) self.assertEqual(list(message.repeated_string), ['c', 'bb', 'aaa']) @@ -499,7 +502,7 @@ class MessageTest(unittest.TestCase): m2.repeated_nested_message.add().bb = 2 m2.repeated_nested_message.add().bb = 3 - if sys.version_info.major >= 3: return # No cmp() in PY3. + if sys.version_info >= (3,): return # No cmp() in PY3. # These comparisons should not raise errors. _ = m1 < m2 @@ -1248,14 +1251,14 @@ class Proto3Test(unittest.TestCase): msg = map_unittest_pb2.TestMap() self.assertIsNone(msg.map_int32_int32.get(5)) - self.assertEquals(10, msg.map_int32_int32.get(5, 10)) + self.assertEqual(10, msg.map_int32_int32.get(5, 10)) self.assertIsNone(msg.map_int32_int32.get(5)) msg.map_int32_int32[5] = 15 - self.assertEquals(15, msg.map_int32_int32.get(5)) + self.assertEqual(15, msg.map_int32_int32.get(5)) self.assertIsNone(msg.map_int32_foreign_message.get(5)) - self.assertEquals(10, msg.map_int32_foreign_message.get(5, 10)) + self.assertEqual(10, msg.map_int32_foreign_message.get(5, 10)) submsg = msg.map_int32_foreign_message[5] self.assertIs(submsg, msg.map_int32_foreign_message.get(5)) diff --git a/python/google/protobuf/internal/proto_builder_test.py b/python/google/protobuf/internal/proto_builder_test.py index edaf3fa358..1eda10fbb9 100644 --- a/python/google/protobuf/internal/proto_builder_test.py +++ b/python/google/protobuf/internal/proto_builder_test.py @@ -32,8 +32,15 @@ """Tests for google.protobuf.proto_builder.""" -import collections -import unittest +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict #PY26 + +try: + import unittest2 as unittest #PY26 +except ImportError: + import unittest from google.protobuf import descriptor_pb2 from google.protobuf import descriptor_pool @@ -44,7 +51,7 @@ from google.protobuf import text_format class ProtoBuilderTest(unittest.TestCase): def setUp(self): - self.ordered_fields = collections.OrderedDict([ + self.ordered_fields = OrderedDict([ ('foo', descriptor_pb2.FieldDescriptorProto.TYPE_INT64), ('bar', descriptor_pb2.FieldDescriptorProto.TYPE_STRING), ]) diff --git a/python/google/protobuf/internal/reflection_test.py b/python/google/protobuf/internal/reflection_test.py index 9fe3abee61..8967dc2e39 100755 --- a/python/google/protobuf/internal/reflection_test.py +++ b/python/google/protobuf/internal/reflection_test.py @@ -39,18 +39,10 @@ import copy import gc import operator import struct -import unittest try: - from unittest import skipIf + import unittest2 as unittest except ImportError: - def skipIf(predicate, message): - def decorator(wrapped): - if predicate: - def _noop(*args, **kw): - pass - return _noop - return wrapped - return decorator + import unittest import six @@ -1634,7 +1626,7 @@ class ReflectionTest(unittest.TestCase): self.assertFalse(proto.IsInitialized(errors)) self.assertEqual(errors, ['a', 'b', 'c']) - @skipIf( + @unittest.skipIf( api_implementation.Type() != 'cpp' or api_implementation.Version() != 2, 'Errors are only available from the most recent C++ implementation.') def testFileDescriptorErrors(self): @@ -2835,7 +2827,7 @@ class OptionsTest(unittest.TestCase): class ClassAPITest(unittest.TestCase): - @skipIf( + @unittest.skipIf( api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, 'C++ implementation requires a call to MakeDescriptor()') def testMakeClassWithNestedDescriptor(self): diff --git a/python/google/protobuf/internal/service_reflection_test.py b/python/google/protobuf/internal/service_reflection_test.py index 9967255a19..d7cd591bc5 100755 --- a/python/google/protobuf/internal/service_reflection_test.py +++ b/python/google/protobuf/internal/service_reflection_test.py @@ -34,7 +34,10 @@ __author__ = 'petar@google.com (Petar Petrov)' -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf import unittest_pb2 from google.protobuf import service_reflection from google.protobuf import service diff --git a/python/google/protobuf/internal/symbol_database_test.py b/python/google/protobuf/internal/symbol_database_test.py index b2489cdb18..9744226296 100644 --- a/python/google/protobuf/internal/symbol_database_test.py +++ b/python/google/protobuf/internal/symbol_database_test.py @@ -32,7 +32,10 @@ """Tests for google.protobuf.symbol_database.""" -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf import unittest_pb2 from google.protobuf import symbol_database diff --git a/python/google/protobuf/internal/text_encoding_test.py b/python/google/protobuf/internal/text_encoding_test.py index 9e7b9ce433..338a287b1b 100755 --- a/python/google/protobuf/internal/text_encoding_test.py +++ b/python/google/protobuf/internal/text_encoding_test.py @@ -32,7 +32,10 @@ """Tests for google.protobuf.text_encoding.""" -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf import text_encoding TEST_VALUES = [ diff --git a/python/google/protobuf/internal/text_format_test.py b/python/google/protobuf/internal/text_format_test.py index 49e6332c2d..67d08dfe87 100755 --- a/python/google/protobuf/internal/text_format_test.py +++ b/python/google/protobuf/internal/text_format_test.py @@ -38,7 +38,10 @@ import re import six -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf.internal import _parameterized from google.protobuf import map_unittest_pb2 @@ -62,7 +65,7 @@ class TextFormatBase(unittest.TestCase): self.assertMultiLineEqual(text, ''.join(golden_lines)) def CompareToGoldenText(self, text, golden_text): - self.assertMultiLineEqual(text, golden_text) + self.assertEqual(text, golden_text) def RemoveRedundantZeros(self, text): # Some platforms print 1e+5 as 1e+005. This is fine, but we need to remove @@ -218,13 +221,13 @@ class TextFormatTest(TextFormatBase): text_message = text_format.MessageToString(message, float_format='.15g') self.CompareToGoldenText( self.RemoveRedundantZeros(text_message), - 'payload {{\n {}\n {}\n {}\n {}\n}}\n'.format(*formatted_fields)) + 'payload {{\n {0}\n {1}\n {2}\n {3}\n}}\n'.format(*formatted_fields)) # as_one_line=True is a separate code branch where float_format is passed. text_message = text_format.MessageToString(message, as_one_line=True, float_format='.15g') self.CompareToGoldenText( self.RemoveRedundantZeros(text_message), - 'payload {{ {} {} {} {} }}'.format(*formatted_fields)) + 'payload {{ {0} {1} {2} {3} }}'.format(*formatted_fields)) def testMessageToString(self, message_module): message = message_module.ForeignMessage() @@ -297,7 +300,7 @@ class TextFormatTest(TextFormatBase): def testParseSingleWord(self, message_module): message = message_module.TestAllTypes() text = 'foo' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, (r'1:1 : Message type "\w+.TestAllTypes" has no field named ' r'"foo".'), @@ -306,7 +309,7 @@ class TextFormatTest(TextFormatBase): def testParseUnknownField(self, message_module): message = message_module.TestAllTypes() text = 'unknown_field: 8\n' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, (r'1:1 : Message type "\w+.TestAllTypes" has no field named ' r'"unknown_field".'), @@ -315,7 +318,7 @@ class TextFormatTest(TextFormatBase): def testParseBadEnumValue(self, message_module): message = message_module.TestAllTypes() text = 'optional_nested_enum: BARR' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, (r'1:23 : Enum type "\w+.TestAllTypes.NestedEnum" ' r'has no value named BARR.'), @@ -323,7 +326,7 @@ class TextFormatTest(TextFormatBase): message = message_module.TestAllTypes() text = 'optional_nested_enum: 100' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, (r'1:23 : Enum type "\w+.TestAllTypes.NestedEnum" ' r'has no value with number 100.'), @@ -332,7 +335,7 @@ class TextFormatTest(TextFormatBase): def testParseBadIntValue(self, message_module): message = message_module.TestAllTypes() text = 'optional_int32: bork' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, ('1:17 : Couldn\'t parse integer: bork'), text_format.Parse, text, message) @@ -596,12 +599,12 @@ class Proto2Tests(TextFormatBase): def testParseBadExtension(self): message = unittest_pb2.TestAllExtensions() text = '[unknown_extension]: 8\n' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, '1:2 : Extension "unknown_extension" not registered.', text_format.Parse, text, message) message = unittest_pb2.TestAllTypes() - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, ('1:2 : Message type "protobuf_unittest.TestAllTypes" does not have ' 'extensions.'), @@ -620,7 +623,7 @@ class Proto2Tests(TextFormatBase): message = unittest_pb2.TestAllExtensions() text = ('[protobuf_unittest.optional_int32_extension]: 42 ' '[protobuf_unittest.optional_int32_extension]: 67') - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, ('1:96 : Message type "protobuf_unittest.TestAllExtensions" ' 'should not have multiple ' @@ -631,7 +634,7 @@ class Proto2Tests(TextFormatBase): message = unittest_pb2.TestAllTypes() text = ('optional_nested_message { bb: 1 } ' 'optional_nested_message { bb: 2 }') - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, ('1:65 : Message type "protobuf_unittest.TestAllTypes.NestedMessage" ' 'should not have multiple "bb" fields.'), @@ -641,7 +644,7 @@ class Proto2Tests(TextFormatBase): message = unittest_pb2.TestAllTypes() text = ('optional_int32: 42 ' 'optional_int32: 67') - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, ('1:36 : Message type "protobuf_unittest.TestAllTypes" should not ' 'have multiple "optional_int32" fields.'), @@ -650,11 +653,11 @@ class Proto2Tests(TextFormatBase): def testParseGroupNotClosed(self): message = unittest_pb2.TestAllTypes() text = 'RepeatedGroup: <' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, '1:16 : Expected ">".', text_format.Parse, text, message) text = 'RepeatedGroup: {' - self.assertRaisesRegexp( + six.assertRaisesRegex(self, text_format.ParseError, '1:16 : Expected "}".', text_format.Parse, text, message) diff --git a/python/google/protobuf/internal/unknown_fields_test.py b/python/google/protobuf/internal/unknown_fields_test.py index 5cd23d7892..1b5d7bc04d 100755 --- a/python/google/protobuf/internal/unknown_fields_test.py +++ b/python/google/protobuf/internal/unknown_fields_test.py @@ -35,18 +35,10 @@ __author__ = 'bohdank@google.com (Bohdan Koval)' -import unittest try: - from unittest import skipIf + import unittest2 as unittest except ImportError: - def skipIf(predicate, message): - def decorator(wrapped): - if predicate: - def _noop(*args, **kw): - pass - return _noop - return wrapped - return decorator + import unittest from google.protobuf import unittest_mset_pb2 from google.protobuf import unittest_pb2 @@ -129,7 +121,7 @@ class UnknownFieldsTest(unittest.TestCase): self.assertNotEqual(self.empty_message, message) -@skipIf( +@unittest.skipIf( api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, 'C++ implementation does not expose unknown fields to Python') class UnknownFieldsAccessorsTest(unittest.TestCase): @@ -217,7 +209,7 @@ class UnknownFieldsAccessorsTest(unittest.TestCase): -@skipIf( +@unittest.skipIf( api_implementation.Type() == 'cpp' and api_implementation.Version() == 2, 'C++ implementation does not expose unknown fields to Python') class UnknownEnumValuesTest(unittest.TestCase): diff --git a/python/google/protobuf/internal/wire_format_test.py b/python/google/protobuf/internal/wire_format_test.py index 78dc11670f..f659d18e42 100755 --- a/python/google/protobuf/internal/wire_format_test.py +++ b/python/google/protobuf/internal/wire_format_test.py @@ -34,7 +34,10 @@ __author__ = 'robinson@google.com (Will Robinson)' -import unittest +try: + import unittest2 as unittest +except ImportError: + import unittest from google.protobuf import message from google.protobuf.internal import wire_format diff --git a/python/google/protobuf/proto_builder.py b/python/google/protobuf/proto_builder.py index 7489cf63ab..700e3c25e5 100644 --- a/python/google/protobuf/proto_builder.py +++ b/python/google/protobuf/proto_builder.py @@ -30,7 +30,10 @@ """Dynamic Protobuf class creator.""" -import collections +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict #PY26 import hashlib import os @@ -80,7 +83,7 @@ def MakeSimpleProtoClass(fields, full_name, pool=None): # an OrderedDict we keep the order, but otherwise we sort the field to ensure # consistent ordering. field_items = fields.items() - if not isinstance(fields, collections.OrderedDict): + if not isinstance(fields, OrderedDict): field_items = sorted(field_items) # Use a consistent file name that is unlikely to conflict with any imported diff --git a/python/setup.py b/python/setup.py index bb8a7d0542..79e236987f 100755 --- a/python/setup.py +++ b/python/setup.py @@ -158,6 +158,11 @@ if __name__ == '__main__': ) os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'cpp' + install_requires = ['six', 'setuptools'] + if sys.version_info <= (2,7): + install_requires.append('ordereddict') + install_requires.append('unittest2') + setup( name='protobuf', version=GetVersion(), @@ -187,6 +192,6 @@ if __name__ == '__main__': 'clean': clean, 'build_py': build_py, }, - install_requires=['setuptools', 'six'], + install_requires=install_requires, ext_modules=ext_module_list, )