Get Python 2.6 working.

Signed-off-by: Dan O'Reilly <oreilldf@gmail.com>
pull/722/head
Dan O'Reilly 9 years ago
parent d06adbd4a4
commit 2621c8aefb
  1. 3
      python/google/protobuf/internal/_parameterized.py
  2. 3
      python/google/protobuf/internal/descriptor_database_test.py
  3. 4
      python/google/protobuf/internal/descriptor_pool_test.py
  4. 10
      python/google/protobuf/internal/descriptor_test.py
  5. 5
      python/google/protobuf/internal/generator_test.py
  6. 3
      python/google/protobuf/internal/message_factory_test.py
  7. 19
      python/google/protobuf/internal/message_test.py
  8. 11
      python/google/protobuf/internal/proto_builder_test.py
  9. 16
      python/google/protobuf/internal/reflection_test.py
  10. 3
      python/google/protobuf/internal/service_reflection_test.py
  11. 3
      python/google/protobuf/internal/symbol_database_test.py
  12. 3
      python/google/protobuf/internal/text_encoding_test.py
  13. 33
      python/google/protobuf/internal/text_format_test.py
  14. 16
      python/google/protobuf/internal/unknown_fields_test.py
  15. 3
      python/google/protobuf/internal/wire_format_test.py
  16. 7
      python/google/protobuf/proto_builder.py
  17. 7
      python/setup.py

@ -149,6 +149,9 @@ import collections
import functools
import re
import types
try:
import unittest2 as unittest
except ImportError:
import unittest
import uuid

@ -34,6 +34,9 @@
__author__ = 'matthewtoia@google.com (Matt Toia)'
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf import descriptor_pb2
from google.protobuf.internal import factory_test2_pb2

@ -35,9 +35,11 @@
__author__ = 'matthewtoia@google.com (Matt Toia)'
import os
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

@ -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:])

@ -41,6 +41,9 @@ further ensures that we can use Python protocol message objects as we expect.
__author__ = 'robinson@google.com (Will Robinson)'
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
@ -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,

@ -34,6 +34,9 @@
__author__ = 'matthewtoia@google.com (Matt Toia)'
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf import descriptor_pb2
from google.protobuf.internal import factory_test1_pb2

@ -54,6 +54,9 @@ import six
if six.PY3:
long = int
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf.internal import _parameterized
from google.protobuf import map_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))

@ -32,7 +32,14 @@
"""Tests for google.protobuf.proto_builder."""
import collections
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
@ -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),
])

@ -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):

@ -34,6 +34,9 @@
__author__ = 'petar@google.com (Petar Petrov)'
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf import unittest_pb2
from google.protobuf import service_reflection

@ -32,6 +32,9 @@
"""Tests for google.protobuf.symbol_database."""
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf import unittest_pb2
from google.protobuf import symbol_database

@ -32,6 +32,9 @@
"""Tests for google.protobuf.text_encoding."""
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf import text_encoding

@ -38,6 +38,9 @@ import re
import six
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf.internal import _parameterized
@ -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)

@ -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):

@ -34,6 +34,9 @@
__author__ = 'robinson@google.com (Will Robinson)'
try:
import unittest2 as unittest
except ImportError:
import unittest
from google.protobuf import message
from google.protobuf.internal import wire_format

@ -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

@ -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,
)

Loading…
Cancel
Save