Permalink
Browse files

Added support for Python 3. Note that Python 2.7+ or Python 3.3+ are …

…required. The distribution only builds for a single interpreter (but is source-compatible with both targets).
  • Loading branch information...
2 parents 7a45b85 + b8a65c4 commit ca3abfb7340b1c5dddf1ab18482b668bb409be59 @malthe committed Mar 20, 2013
View
6 CHANGES.txt
@@ -1,3 +1,9 @@
+In next release ...
+
+ Python
+ * Added support for Python 3. Note that we now require major and
+ minor release 2.7+ and 3.3+ respectively.
+
2013-02-27 version 2.5.0:
General
View
7 CONTRIBUTORS.txt
@@ -88,3 +88,10 @@ Patch contributors:
* Added CodedInputStream.getTotalBytesRead().
Kacper Kowalik <xarthisius.kk@gmail.com>
* Fixed m4/acx_pthread.m4 problem for some Linux distributions.
+ Charles Law <charles.law@openx.com>
+ * Patches toward Python 3 compatibility.
+ Malthe Borch <mborch@gmail.com>
+ * Added support for Python 3. Note that Python 2.7+ is now the
+ only supported minor release on the 2.x-series, while 3.3+ is
+ required on the 3.x-series (for syntax compatibility).
+
View
2 python/README.txt
@@ -26,7 +26,7 @@ join the Protocol Buffers discussion list and let us know!
Installation
============
-1) Make sure you have Python 2.4 or newer. If in doubt, run:
+1) Make sure you have Python 2.7 or newer. If in doubt, run:
$ python -V
View
62 python/google/protobuf/internal/containers.py
@@ -41,6 +41,8 @@
__author__ = 'petar@google.com (Petar Petrov)'
+from google.protobuf.internal.utils import cmp
+
class BaseContainer(object):
@@ -78,13 +80,8 @@ def __hash__(self):
def __repr__(self):
return repr(self._values)
- def sort(self, *args, **kwargs):
- # Continue to support the old sort_function keyword argument.
- # This is expected to be a rare occurrence, so use LBYL to avoid
- # the overhead of actually catching KeyError.
- if 'sort_function' in kwargs:
- kwargs['cmp'] = kwargs.pop('sort_function')
- self._values.sort(*args, **kwargs)
+ def sort(self, sort_function=cmp):
+ self._values.sort(sort_function)
class RepeatedScalarFieldContainer(BaseContainer):
@@ -146,12 +143,30 @@ def remove(self, elem):
def __setitem__(self, key, value):
"""Sets the item on the specified position."""
- self._type_checker.CheckValue(value)
- self._values[key] = value
+ if isinstance(key, slice):
+ new_values = []
+ for val in value:
+ self._type_checker.CheckValue(val)
+ new_values.append(val)
+ self._values[key.start:key.stop] = new_values
+ self._message_listener.Modified()
+ else:
+ self._type_checker.CheckValue(value)
+ self._values[key] = value
+ self._message_listener.Modified()
+
+ def __getitem__(self, key):
+ """Retrieves the subset of items from between the specified indices."""
+ if isinstance(key, slice):
+ return self._values[key.start:key.stop]
+ else:
+ return super(RepeatedScalarFieldContainer, self).__getitem__(key)
+
+ def __delslice__(self, start, stop):
+ del self._values[start:stop]
self._message_listener.Modified()
def __getslice__(self, start, stop):
- """Retrieves the subset of items from between the specified indices."""
return self._values[start:stop]
def __setslice__(self, start, stop, values):
@@ -164,13 +179,11 @@ def __setslice__(self, start, stop, values):
self._message_listener.Modified()
def __delitem__(self, key):
- """Deletes the item at the specified position."""
- del self._values[key]
- self._message_listener.Modified()
-
- def __delslice__(self, start, stop):
"""Deletes the subset of items from between the specified indices."""
- del self._values[start:stop]
+ if isinstance(key, slice):
+ del self._values[key.start:key.stop]
+ else:
+ del self._values[key]
self._message_listener.Modified()
def __eq__(self, other):
@@ -245,18 +258,19 @@ def remove(self, elem):
self._values.remove(elem)
self._message_listener.Modified()
- def __getslice__(self, start, stop):
+ def __getitem__(self, key):
"""Retrieves the subset of items from between the specified indices."""
- return self._values[start:stop]
+ if isinstance(key, slice):
+ return self._values[key.start:key.stop]
+ else:
+ return super(RepeatedCompositeFieldContainer, self).__getitem__(key)
def __delitem__(self, key):
- """Deletes the item at the specified position."""
- del self._values[key]
- self._message_listener.Modified()
-
- def __delslice__(self, start, stop):
"""Deletes the subset of items from between the specified indices."""
- del self._values[start:stop]
+ if isinstance(key, slice):
+ del self._values[key.start:key.stop]
+ else:
+ del self._values[key]
self._message_listener.Modified()
def __eq__(self, other):
View
72 python/google/protobuf/internal/cpp_message.py
@@ -34,10 +34,22 @@
__author__ = 'petar@google.com (Petar Petrov)'
-import copy_reg
+import sys
import operator
+
+try:
+ import copy_reg
+except ImportError:
+ import copyreg as copy_reg
+
+if sys.version_info[0] == 3:
+ iteritems = dict.items
+else:
+ iteritems = dict.iteritems
+
from google.protobuf.internal import _net_proto2___python
from google.protobuf.internal import enum_type_wrapper
+from google.protobuf.internal.utils import cmp
from google.protobuf import message
@@ -158,12 +170,10 @@ def __ne__(self, other):
def __hash__(self):
raise TypeError('unhashable object')
- def sort(self, *args, **kwargs):
- # Maintain compatibility with the previous interface.
- if 'sort_function' in kwargs:
- kwargs['cmp'] = kwargs.pop('sort_function')
- self._cmsg.AssignRepeatedScalar(self._cfield_descriptor,
- sorted(self, *args, **kwargs))
+ def sort(self, sort_function=cmp):
+ values = self[slice(None, None, None)]
+ values.sort(sort_function)
+ self._cmsg.AssignRepeatedScalar(self._cfield_descriptor, values)
def RepeatedScalarProperty(cdescriptor):
@@ -246,29 +256,27 @@ def __eq__(self, other):
def __hash__(self):
raise TypeError('unhashable object')
- def sort(self, cmp=None, key=None, reverse=False, **kwargs):
- # Maintain compatibility with the old interface.
- if cmp is None and 'sort_function' in kwargs:
- cmp = kwargs.pop('sort_function')
-
- # The cmp function, if provided, is passed the results of the key function,
- # so we only need to wrap one of them.
- if key is None:
- index_key = self.__getitem__
- else:
- index_key = lambda i: key(self[i])
+ def sort(self, sort_function=cmp):
+ messages = []
+ for index in range(len(self)):
+ # messages[i][0] is where the i-th element of the new array has to come
+ # from.
+ # messages[i][1] is where the i-th element of the old array has to go.
+ messages.append([index, 0, self[index]])
+ messages.sort(lambda x,y: sort_function(x[2], y[2]))
- # Sort the list of current indexes by the underlying object.
- indexes = range(len(self))
- indexes.sort(cmp=cmp, key=index_key, reverse=reverse)
+ # Remember which position each elements has to move to.
+ for i in range(len(messages)):
+ messages[messages[i][0]][1] = i
# Apply the transposition.
- for dest, src in enumerate(indexes):
- if dest == src:
+ for i in range(len(messages)):
+ from_position = messages[i][0]
+ if i == from_position:
continue
- self._cmsg.SwapRepeatedFieldElements(self._cfield_descriptor, dest, src)
- # Don't swap the same value twice.
- indexes[src] = src
+ self._cmsg.SwapRepeatedFieldElements(
+ self._cfield_descriptor, i, from_position)
+ messages[messages[i][1]][0] = from_position
def RepeatedCompositeProperty(cdescriptor, message_type):
@@ -420,7 +428,7 @@ def _AddEnumValues(message_descriptor, dictionary):
def _AddClassAttributesForNestedExtensions(message_descriptor, dictionary):
"""Adds class attributes for the nested extensions."""
extension_dict = message_descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in iteritems(extension_dict):
assert extension_name not in dictionary
dictionary[extension_name] = extension_field
@@ -454,7 +462,7 @@ def _AddInitMethod(message_descriptor, cls):
def Init(self, **kwargs):
"""Message constructor."""
cmessage = kwargs.pop('__cmessage', None)
- if cmessage:
+ if cmessage is not None:
self._cmsg = cmessage
else:
self._cmsg = NewCMessage(message_descriptor.full_name)
@@ -474,7 +482,7 @@ def Init(self, **kwargs):
self._HACK_REFCOUNTS = self
self._composite_fields = {}
- for field_name, field_value in kwargs.iteritems():
+ for field_name, field_value in iteritems(kwargs):
field_cdescriptor = self.__descriptors.get(field_name, None)
if not field_cdescriptor:
raise ValueError('Protocol message has no "%s" field.' % field_name)
@@ -538,7 +546,7 @@ def ClearField(self, field_name):
def Clear(self):
cmessages_to_release = []
- for field_name, child_field in self._composite_fields.iteritems():
+ for field_name, child_field in iteritems(self._composite_fields):
child_cdescriptor = self.__descriptors[field_name]
# TODO(anuraag): Support clearing repeated message fields as well.
if (child_cdescriptor.label != _LABEL_REPEATED and
@@ -631,7 +639,7 @@ def __unicode__(self):
return text_format.MessageToString(self, as_utf8=True).decode('utf-8')
# Attach the local methods to the message class.
- for key, value in locals().copy().iteritems():
+ for key, value in iteritems(locals().copy()):
if key not in ('key', 'value', '__builtins__', '__name__', '__doc__'):
setattr(cls, key, value)
@@ -658,6 +666,6 @@ def FromString(string):
def _AddPropertiesForExtensions(message_descriptor, cls):
"""Adds properties for all fields in this protocol message type."""
extension_dict = message_descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in iteritems(extension_dict):
constant_name = extension_name.upper() + '_FIELD_NUMBER'
setattr(cls, constant_name, extension_field.number)
View
37 python/google/protobuf/internal/decoder.py
@@ -80,19 +80,19 @@
__author__ = 'kenton@google.com (Kenton Varda)'
+import sys
import struct
+
from google.protobuf.internal import encoder
from google.protobuf.internal import wire_format
from google.protobuf import message
-
# This will overflow and thus become IEEE-754 "infinity". We would use
# "float('inf')" but it doesn't work on Windows pre-Python-2.6.
_POS_INF = 1e10000
_NEG_INF = -_POS_INF
_NAN = _POS_INF * 0
-
# This is not for optimization, but rather to avoid conflicts with local
# variables named "message".
_DecodeError = message.DecodeError
@@ -113,7 +113,7 @@ def DecodeVarint(buffer, pos):
result = 0
shift = 0
while 1:
- b = local_ord(buffer[pos])
+ b = local_ord(buffer[pos:pos + 1])
result |= ((b & 0x7f) << shift)
pos += 1
if not (b & 0x80):
@@ -133,7 +133,7 @@ def DecodeVarint(buffer, pos):
result = 0
shift = 0
while 1:
- b = local_ord(buffer[pos])
+ b = local_ord(buffer[pos:pos + 1])
result |= ((b & 0x7f) << shift)
pos += 1
if not (b & 0x80):
@@ -169,7 +169,7 @@ def ReadTag(buffer, pos):
"""
start = pos
- while ord(buffer[pos]) & 0x80:
+ while ord(buffer[pos:pos + 1]) & 0x80:
pos += 1
pos += 1
return (buffer[start:pos], pos)
@@ -294,13 +294,13 @@ def InnerDecode(buffer, pos):
# If this value has all its exponent bits set, then it's non-finite.
# In Python 2.4, struct.unpack will convert it to a finite 64-bit value.
# To avoid that, we parse it specially.
- if ((float_bytes[3] in '\x7F\xFF')
- and (float_bytes[2] >= '\x80')):
+ if ((float_bytes[3] in b'\x7F\xFF')
+ and (float_bytes[2] >= b'\x80')):
# If at least one significand bit is set...
- if float_bytes[0:3] != '\x00\x00\x80':
+ if float_bytes[0:3] != b'\x00\x00\x80':
return (_NAN, new_pos)
# If sign bit is set...
- if float_bytes[3] == '\xFF':
+ if float_bytes[3] == b'\xFF':
return (_NEG_INF, new_pos)
return (_POS_INF, new_pos)
@@ -329,9 +329,9 @@ def InnerDecode(buffer, pos):
# If this value has all its exponent bits set and at least one significand
# bit set, it's not a number. In Python 2.4, struct.unpack will treat it
# as inf or -inf. To avoid that, we treat it specially.
- if ((double_bytes[7] in '\x7F\xFF')
- and (double_bytes[6] >= '\xF0')
- and (double_bytes[0:7] != '\x00\x00\x00\x00\x00\x00\xF0')):
+ if ((double_bytes[7] in b'\x7F\xFF')
+ and (double_bytes[6] >= b'\xF0')
+ and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
return (_NAN, new_pos)
# Note that we expect someone up-stack to catch struct.error and convert
@@ -376,9 +376,7 @@ def InnerDecode(buffer, pos):
def StringDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a string field."""
-
local_DecodeVarint = _DecodeVarint
- local_unicode = unicode
assert not is_packed
if is_repeated:
@@ -394,7 +392,7 @@ def DecodeRepeatedField(buffer, pos, end, message, field_dict):
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
- value.append(local_unicode(buffer[pos:new_pos], 'utf-8'))
+ value.append(buffer[pos:new_pos].decode('utf-8'))
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
@@ -407,7 +405,7 @@ def DecodeField(buffer, pos, end, message, field_dict):
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
- field_dict[key] = local_unicode(buffer[pos:new_pos], 'utf-8')
+ field_dict[key] = buffer[pos:new_pos].decode('utf-8')
return new_pos
return DecodeField
@@ -632,7 +630,7 @@ def DecodeItem(buffer, pos, end, message, field_dict):
def _SkipVarint(buffer, pos, end):
"""Skip a varint value. Returns the new position."""
- while ord(buffer[pos]) & 0x80:
+ while ord(buffer[pos:pos + 1]) & 0x80:
pos += 1
pos += 1
if pos > end:
@@ -699,7 +697,10 @@ def _FieldSkipper():
]
wiretype_mask = wire_format.TAG_TYPE_MASK
- local_ord = ord
+ if sys.version_info[0] == 3:
+ local_ord = int
+ else:
+ local_ord = ord
def SkipField(buffer, pos, end, tag_bytes):
"""Skips a field with the specified tag.
View
76 python/google/protobuf/internal/descriptor_pool_test.py
@@ -58,15 +58,15 @@ def testFindFileByName(self):
name1 = 'net/proto2/python/internal/factory_test1.proto'
file_desc1 = self.pool.FindFileByName(name1)
self.assertIsInstance(file_desc1, descriptor.FileDescriptor)
- self.assertEquals(name1, file_desc1.name)
- self.assertEquals('net.proto2.python.internal', file_desc1.package)
+ self.assertEqual(name1, file_desc1.name)
+ self.assertEqual('net.proto2.python.internal', file_desc1.package)
self.assertIn('Factory1Message', file_desc1.message_types_by_name)
name2 = 'net/proto2/python/internal/factory_test2.proto'
file_desc2 = self.pool.FindFileByName(name2)
self.assertIsInstance(file_desc2, descriptor.FileDescriptor)
- self.assertEquals(name2, file_desc2.name)
- self.assertEquals('net.proto2.python.internal', file_desc2.package)
+ self.assertEqual(name2, file_desc2.name)
+ self.assertEqual('net.proto2.python.internal', file_desc2.package)
self.assertIn('Factory2Message', file_desc2.message_types_by_name)
def testFindFileByNameFailure(self):
@@ -80,17 +80,17 @@ def testFindFileContainingSymbol(self):
file_desc1 = self.pool.FindFileContainingSymbol(
'net.proto2.python.internal.Factory1Message')
self.assertIsInstance(file_desc1, descriptor.FileDescriptor)
- self.assertEquals('net/proto2/python/internal/factory_test1.proto',
+ self.assertEqual('net/proto2/python/internal/factory_test1.proto',
file_desc1.name)
- self.assertEquals('net.proto2.python.internal', file_desc1.package)
+ self.assertEqual('net.proto2.python.internal', file_desc1.package)
self.assertIn('Factory1Message', file_desc1.message_types_by_name)
file_desc2 = self.pool.FindFileContainingSymbol(
'net.proto2.python.internal.Factory2Message')
self.assertIsInstance(file_desc2, descriptor.FileDescriptor)
- self.assertEquals('net/proto2/python/internal/factory_test2.proto',
+ self.assertEqual('net/proto2/python/internal/factory_test2.proto',
file_desc2.name)
- self.assertEquals('net.proto2.python.internal', file_desc2.package)
+ self.assertEqual('net.proto2.python.internal', file_desc2.package)
self.assertIn('Factory2Message', file_desc2.message_types_by_name)
def testFindFileContainingSymbolFailure(self):
@@ -104,67 +104,67 @@ def testFindMessageTypeByName(self):
msg1 = self.pool.FindMessageTypeByName(
'net.proto2.python.internal.Factory1Message')
self.assertIsInstance(msg1, descriptor.Descriptor)
- self.assertEquals('Factory1Message', msg1.name)
- self.assertEquals('net.proto2.python.internal.Factory1Message',
+ self.assertEqual('Factory1Message', msg1.name)
+ self.assertEqual('net.proto2.python.internal.Factory1Message',
msg1.full_name)
- self.assertEquals(None, msg1.containing_type)
+ self.assertEqual(None, msg1.containing_type)
nested_msg1 = msg1.nested_types[0]
- self.assertEquals('NestedFactory1Message', nested_msg1.name)
- self.assertEquals(msg1, nested_msg1.containing_type)
+ self.assertEqual('NestedFactory1Message', nested_msg1.name)
+ self.assertEqual(msg1, nested_msg1.containing_type)
nested_enum1 = msg1.enum_types[0]
- self.assertEquals('NestedFactory1Enum', nested_enum1.name)
- self.assertEquals(msg1, nested_enum1.containing_type)
+ self.assertEqual('NestedFactory1Enum', nested_enum1.name)
+ self.assertEqual(msg1, nested_enum1.containing_type)
- self.assertEquals(nested_msg1, msg1.fields_by_name[
+ self.assertEqual(nested_msg1, msg1.fields_by_name[
'nested_factory_1_message'].message_type)
- self.assertEquals(nested_enum1, msg1.fields_by_name[
+ self.assertEqual(nested_enum1, msg1.fields_by_name[
'nested_factory_1_enum'].enum_type)
msg2 = self.pool.FindMessageTypeByName(
'net.proto2.python.internal.Factory2Message')
self.assertIsInstance(msg2, descriptor.Descriptor)
- self.assertEquals('Factory2Message', msg2.name)
- self.assertEquals('net.proto2.python.internal.Factory2Message',
+ self.assertEqual('Factory2Message', msg2.name)
+ self.assertEqual('net.proto2.python.internal.Factory2Message',
msg2.full_name)
self.assertIsNone(msg2.containing_type)
nested_msg2 = msg2.nested_types[0]
- self.assertEquals('NestedFactory2Message', nested_msg2.name)
- self.assertEquals(msg2, nested_msg2.containing_type)
+ self.assertEqual('NestedFactory2Message', nested_msg2.name)
+ self.assertEqual(msg2, nested_msg2.containing_type)
nested_enum2 = msg2.enum_types[0]
- self.assertEquals('NestedFactory2Enum', nested_enum2.name)
- self.assertEquals(msg2, nested_enum2.containing_type)
+ self.assertEqual('NestedFactory2Enum', nested_enum2.name)
+ self.assertEqual(msg2, nested_enum2.containing_type)
- self.assertEquals(nested_msg2, msg2.fields_by_name[
+ self.assertEqual(nested_msg2, msg2.fields_by_name[
'nested_factory_2_message'].message_type)
- self.assertEquals(nested_enum2, msg2.fields_by_name[
+ self.assertEqual(nested_enum2, msg2.fields_by_name[
'nested_factory_2_enum'].enum_type)
self.assertTrue(msg2.fields_by_name['int_with_default'].has_default)
- self.assertEquals(
+ self.assertEqual(
1776, msg2.fields_by_name['int_with_default'].default_value)
self.assertTrue(msg2.fields_by_name['double_with_default'].has_default)
- self.assertEquals(
+ self.assertEqual(
9.99, msg2.fields_by_name['double_with_default'].default_value)
self.assertTrue(msg2.fields_by_name['string_with_default'].has_default)
- self.assertEquals(
+ self.assertEqual(
'hello world', msg2.fields_by_name['string_with_default'].default_value)
self.assertTrue(msg2.fields_by_name['bool_with_default'].has_default)
self.assertFalse(msg2.fields_by_name['bool_with_default'].default_value)
self.assertTrue(msg2.fields_by_name['enum_with_default'].has_default)
- self.assertEquals(
+ self.assertEqual(
1, msg2.fields_by_name['enum_with_default'].default_value)
msg3 = self.pool.FindMessageTypeByName(
'net.proto2.python.internal.Factory2Message.NestedFactory2Message')
- self.assertEquals(nested_msg2, msg3)
+ self.assertEqual(nested_msg2, msg3)
def testFindMessageTypeByNameFailure(self):
try:
@@ -177,29 +177,29 @@ def testFindEnumTypeByName(self):
enum1 = self.pool.FindEnumTypeByName(
'net.proto2.python.internal.Factory1Enum')
self.assertIsInstance(enum1, descriptor.EnumDescriptor)
- self.assertEquals(0, enum1.values_by_name['FACTORY_1_VALUE_0'].number)
- self.assertEquals(1, enum1.values_by_name['FACTORY_1_VALUE_1'].number)
+ self.assertEqual(0, enum1.values_by_name['FACTORY_1_VALUE_0'].number)
+ self.assertEqual(1, enum1.values_by_name['FACTORY_1_VALUE_1'].number)
nested_enum1 = self.pool.FindEnumTypeByName(
'net.proto2.python.internal.Factory1Message.NestedFactory1Enum')
self.assertIsInstance(nested_enum1, descriptor.EnumDescriptor)
- self.assertEquals(
+ self.assertEqual(
0, nested_enum1.values_by_name['NESTED_FACTORY_1_VALUE_0'].number)
- self.assertEquals(
+ self.assertEqual(
1, nested_enum1.values_by_name['NESTED_FACTORY_1_VALUE_1'].number)
enum2 = self.pool.FindEnumTypeByName(
'net.proto2.python.internal.Factory2Enum')
self.assertIsInstance(enum2, descriptor.EnumDescriptor)
- self.assertEquals(0, enum2.values_by_name['FACTORY_2_VALUE_0'].number)
- self.assertEquals(1, enum2.values_by_name['FACTORY_2_VALUE_1'].number)
+ self.assertEqual(0, enum2.values_by_name['FACTORY_2_VALUE_0'].number)
+ self.assertEqual(1, enum2.values_by_name['FACTORY_2_VALUE_1'].number)
nested_enum2 = self.pool.FindEnumTypeByName(
'net.proto2.python.internal.Factory2Message.NestedFactory2Enum')
self.assertIsInstance(nested_enum2, descriptor.EnumDescriptor)
- self.assertEquals(
+ self.assertEqual(
0, nested_enum2.values_by_name['NESTED_FACTORY_2_VALUE_0'].number)
- self.assertEquals(
+ self.assertEqual(
1, nested_enum2.values_by_name['NESTED_FACTORY_2_VALUE_1'].number)
def testFindEnumTypeByNameFailure(self):
View
22 python/google/protobuf/internal/descriptor_test.py
@@ -43,7 +43,7 @@
from google.protobuf import text_format
-TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII = """
+TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII = b"""
name: 'TestEmptyMessage'
"""
@@ -244,7 +244,7 @@ def testDifferentCustomOptionTypes(self):
unittest_custom_options_pb2.double_opt])
self.assertEqual("Hello, \"World\"", message_options.Extensions[
unittest_custom_options_pb2.string_opt])
- self.assertEqual("Hello\0World", message_options.Extensions[
+ self.assertEqual(b"Hello\0World", message_options.Extensions[
unittest_custom_options_pb2.bytes_opt])
dummy_enum = unittest_custom_options_pb2.DummyMessageContainingEnum
self.assertEqual(
@@ -421,7 +421,7 @@ def testCopyToProto_EmptyMessage(self):
TEST_EMPTY_MESSAGE_DESCRIPTOR_ASCII)
def testCopyToProto_NestedMessage(self):
- TEST_NESTED_MESSAGE_ASCII = """
+ TEST_NESTED_MESSAGE_ASCII = b"""
name: 'NestedMessage'
field: <
name: 'bb'
@@ -437,7 +437,7 @@ def testCopyToProto_NestedMessage(self):
TEST_NESTED_MESSAGE_ASCII)
def testCopyToProto_ForeignNestedMessage(self):
- TEST_FOREIGN_NESTED_ASCII = """
+ TEST_FOREIGN_NESTED_ASCII = b"""
name: 'TestForeignNested'
field: <
name: 'foreign_nested'
@@ -454,7 +454,7 @@ def testCopyToProto_ForeignNestedMessage(self):
TEST_FOREIGN_NESTED_ASCII)
def testCopyToProto_ForeignEnum(self):
- TEST_FOREIGN_ENUM_ASCII = """
+ TEST_FOREIGN_ENUM_ASCII = b"""
name: 'ForeignEnum'
value: <
name: 'FOREIGN_FOO'
@@ -476,7 +476,7 @@ def testCopyToProto_ForeignEnum(self):
TEST_FOREIGN_ENUM_ASCII)
def testCopyToProto_Options(self):
- TEST_DEPRECATED_FIELDS_ASCII = """
+ TEST_DEPRECATED_FIELDS_ASCII = b"""
name: 'TestDeprecatedFields'
field: <
name: 'deprecated_int32'
@@ -495,7 +495,7 @@ def testCopyToProto_Options(self):
TEST_DEPRECATED_FIELDS_ASCII)
def testCopyToProto_AllExtensions(self):
- TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII = """
+ TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII = b"""
name: 'TestEmptyMessageWithExtensions'
extension_range: <
start: 1
@@ -509,7 +509,7 @@ def testCopyToProto_AllExtensions(self):
TEST_EMPTY_MESSAGE_WITH_EXTENSIONS_ASCII)
def testCopyToProto_SeveralExtensions(self):
- TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII = """
+ TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII = b"""
name: 'TestMultipleExtensionRanges'
extension_range: <
start: 42
@@ -531,7 +531,7 @@ def testCopyToProto_SeveralExtensions(self):
TEST_MESSAGE_WITH_SEVERAL_EXTENSIONS_ASCII)
def testCopyToProto_FileDescriptor(self):
- UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII = ("""
+ UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII = (b"""
name: 'google/protobuf/unittest_import.proto'
package: 'protobuf_unittest_import'
dependency: 'google/protobuf/unittest_import_public.proto'
@@ -545,7 +545,7 @@ def testCopyToProto_FileDescriptor(self):
>
>
""" +
- """enum_type: <
+ b"""enum_type: <
name: 'ImportEnum'
value: <
name: 'IMPORT_FOO'
@@ -573,7 +573,7 @@ def testCopyToProto_FileDescriptor(self):
UNITTEST_IMPORT_FILE_DESCRIPTOR_ASCII)
def testCopyToProto_ServiceDescriptor(self):
- TEST_SERVICE_ASCII = """
+ TEST_SERVICE_ASCII = b"""
name: 'TestService'
method: <
name: 'Foo'
View
35 python/google/protobuf/internal/encoder.py
@@ -66,7 +66,9 @@
__author__ = 'kenton@google.com (Kenton Varda)'
+import sys
import struct
+
from google.protobuf.internal import wire_format
@@ -340,7 +342,11 @@ def FieldSize(value):
def _VarintEncoder():
"""Return an encoder for a basic varint value (does not include tag)."""
- local_chr = chr
+ if sys.version_info[0] == 3:
+ local_chr = lambda i: bytes([i])
+ else:
+ local_chr = chr
+
def EncodeVarint(write, value):
bits = value & 0x7f
value >>= 7
@@ -357,7 +363,11 @@ def _SignedVarintEncoder():
"""Return an encoder for a basic signed varint value (does not include
tag)."""
- local_chr = chr
+ if sys.version_info[0] == 3:
+ local_chr = lambda i: bytes([i])
+ else:
+ local_chr = chr
+
def EncodeSignedVarint(write, value):
if value < 0:
value += (1 << 64)
@@ -382,7 +392,7 @@ def _VarintBytes(value):
pieces = []
_EncodeVarint(pieces.append, value)
- return "".join(pieces)
+ return b"".join(pieces)
def TagBytes(field_number, wire_type):
@@ -525,21 +535,21 @@ def _FloatingPointEncoder(wire_type, format):
def EncodeNonFiniteOrRaise(write, value):
# Remember that the serialized form uses little-endian byte order.
if value == _POS_INF:
- write('\x00\x00\x80\x7F')
+ write(b'\x00\x00\x80\x7F')
elif value == _NEG_INF:
- write('\x00\x00\x80\xFF')
+ write(b'\x00\x00\x80\xFF')
elif value != value: # NaN
- write('\x00\x00\xC0\x7F')
+ write(b'\x00\x00\xC0\x7F')
else:
raise
elif value_size == 8:
def EncodeNonFiniteOrRaise(write, value):
if value == _POS_INF:
- write('\x00\x00\x00\x00\x00\x00\xF0\x7F')
+ write(b'\x00\x00\x00\x00\x00\x00\xF0\x7F')
elif value == _NEG_INF:
- write('\x00\x00\x00\x00\x00\x00\xF0\xFF')
+ write(b'\x00\x00\x00\x00\x00\x00\xF0\xFF')
elif value != value: # NaN
- write('\x00\x00\x00\x00\x00\x00\xF8\x7F')
+ write(b'\x00\x00\x00\x00\x00\x00\xF8\x7F')
else:
raise
else:
@@ -615,8 +625,8 @@ def EncodeField(write, value):
def BoolEncoder(field_number, is_repeated, is_packed):
"""Returns an encoder for a boolean field."""
- false_byte = chr(0)
- true_byte = chr(1)
+ false_byte = b'\x00'
+ true_byte = b'\x01'
if is_packed:
tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
local_EncodeVarint = _EncodeVarint
@@ -679,6 +689,7 @@ def BytesEncoder(field_number, is_repeated, is_packed):
tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)
local_EncodeVarint = _EncodeVarint
local_len = len
+
assert not is_packed
if is_repeated:
def EncodeRepeatedField(write, value):
@@ -752,7 +763,7 @@ def MessageSetItemEncoder(field_number):
}
}
"""
- start_bytes = "".join([
+ start_bytes = b"".join([
TagBytes(1, wire_format.WIRETYPE_START_GROUP),
TagBytes(2, wire_format.WIRETYPE_VARINT),
_VarintBytes(field_number),
View
4 python/google/protobuf/internal/generator_test.py
@@ -102,7 +102,7 @@ def isinf(val):
self.assertTrue(isinf(message.neg_inf_float))
self.assertTrue(message.neg_inf_float < 0)
self.assertTrue(isnan(message.nan_float))
- self.assertEqual("? ? ?? ?? ??? ??/ ??-", message.cpp_trigraph)
+ self.assertEqual(u"? ? ?? ?? ??? ??/ ??-", message.cpp_trigraph)
def testHasDefaultValues(self):
desc = unittest_pb2.TestAllTypes.DESCRIPTOR
@@ -152,7 +152,7 @@ def testMessageWithCustomOptions(self):
# 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,
View
50 python/google/protobuf/internal/message_test.py
@@ -47,22 +47,24 @@
import math
import operator
import pickle
-
import unittest
from google.protobuf import unittest_import_pb2
from google.protobuf import unittest_pb2
-from google.protobuf.internal import api_implementation
from google.protobuf.internal import test_util
-from google.protobuf import message
-
-# Python pre-2.6 does not have isinf() or isnan() functions, so we have
-# to provide our own.
-def isnan(val):
- # NaN is never equal to itself.
- return val != val
-def isinf(val):
- # Infinity times zero equals NaN.
- return not isnan(val) and isnan(val * 0)
+from google.protobuf.internal.utils import cmp
+
+try:
+ from math import isnan, isinf
+except ImportError:
+ #Python pre-2.6 does not have isinf() or isnan() functions, so we have
+ # to provide our own.
+ def isnan(val):
+ # NaN is never equal to itself.
+ return val != val
+ def isinf(val):
+ # Infinity times zero equals NaN.
+ return not isnan(val) and isnan(val * 0)
+
def IsPosInf(val):
return isinf(val) and (val > 0)
def IsNegInf(val):
@@ -75,39 +77,39 @@ def testGoldenMessage(self):
golden_message = unittest_pb2.TestAllTypes()
golden_message.ParseFromString(golden_data)
test_util.ExpectAllFieldsSet(self, golden_message)
- self.assertEqual(golden_data, golden_message.SerializeToString())
+ self.assertTrue(golden_message.SerializeToString() == golden_data)
golden_copy = copy.deepcopy(golden_message)
- self.assertEqual(golden_data, golden_copy.SerializeToString())
+ self.assertTrue(golden_copy.SerializeToString() == golden_data)
def testGoldenExtensions(self):
golden_data = test_util.GoldenFile('golden_message').read()
golden_message = unittest_pb2.TestAllExtensions()
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(all_set)
- self.assertEquals(all_set, golden_message)
- self.assertEqual(golden_data, golden_message.SerializeToString())
+ self.assertEqual(all_set, golden_message)
+ self.assertTrue(golden_message.SerializeToString() == golden_data)
golden_copy = copy.deepcopy(golden_message)
- self.assertEqual(golden_data, golden_copy.SerializeToString())
+ self.assertTrue(golden_copy.SerializeToString() == golden_data)
def testGoldenPackedMessage(self):
golden_data = test_util.GoldenFile('golden_packed_fields_message').read()
golden_message = unittest_pb2.TestPackedTypes()
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestPackedTypes()
test_util.SetAllPackedFields(all_set)
- self.assertEquals(all_set, golden_message)
- self.assertEqual(golden_data, all_set.SerializeToString())
+ self.assertEqual(all_set, golden_message)
+ self.assertTrue(all_set.SerializeToString() == golden_data)
golden_copy = copy.deepcopy(golden_message)
- self.assertEqual(golden_data, golden_copy.SerializeToString())
+ self.assertTrue(golden_copy.SerializeToString() == golden_data)
def testGoldenPackedExtensions(self):
golden_data = test_util.GoldenFile('golden_packed_fields_message').read()
golden_message = unittest_pb2.TestPackedExtensions()
golden_message.ParseFromString(golden_data)
all_set = unittest_pb2.TestPackedExtensions()
test_util.SetAllPackedExtensions(all_set)
- self.assertEquals(all_set, golden_message)
+ self.assertEqual(all_set, golden_message)
self.assertEqual(golden_data, all_set.SerializeToString())
golden_copy = copy.deepcopy(golden_message)
self.assertEqual(golden_data, golden_copy.SerializeToString())
@@ -119,15 +121,15 @@ def testPickleSupport(self):
pickled_message = pickle.dumps(golden_message)
unpickled_message = pickle.loads(pickled_message)
- self.assertEquals(unpickled_message, golden_message)
+ self.assertEqual(unpickled_message, golden_message)
def testPickleIncompleteProto(self):
golden_message = unittest_pb2.TestRequired(a=1)
pickled_message = pickle.dumps(golden_message)
unpickled_message = pickle.loads(pickled_message)
- self.assertEquals(unpickled_message, golden_message)
- self.assertEquals(unpickled_message.a, 1)
+ self.assertEqual(unpickled_message, golden_message)
+ self.assertEqual(unpickled_message.a, 1)
# This is still an incomplete proto - so serializing should fail
self.assertRaises(message.EncodeError, unpickled_message.SerializeToString)
View
32 python/google/protobuf/internal/python_message.py
@@ -28,8 +28,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# This code is meant to work on Python 2.4 and above only.
-#
# TODO(robinson): Helpers for verbose, common checks like seeing if a
# descriptor's cpp_type is CPPTYPE_MESSAGE.
@@ -54,17 +52,25 @@
from cStringIO import StringIO
except ImportError:
try:
- from io import StringIO
+ from io import BytesIO as StringIO
except ImportError:
from StringIO import StringIO
try:
import copy_reg
except ImportError:
- import copyreg
+ import copyreg as copy_reg
+
+import sys
import struct
import weakref
+if sys.version_info[0] == 3:
+ iteritems = dict.items
+ xrange = range
+else:
+ iteritems = dict.iteritems
+
# We use "as" to avoid name collisions with variables.
from google.protobuf.internal import containers
from google.protobuf.internal import decoder
@@ -232,7 +238,7 @@ def AddDecoder(wiretype, is_packed):
def _AddClassAttributesForNestedExtensions(descriptor, dictionary):
extension_dict = descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in extension_dict.items():
assert extension_name not in dictionary
dictionary[extension_name] = extension_field
@@ -314,7 +320,7 @@ def init(self, **kwargs):
self._is_present_in_parent = False
self._listener = message_listener_mod.NullMessageListener()
self._listener_for_children = _Listener(self)
- for field_name, field_value in kwargs.iteritems():
+ for field_name, field_value in kwargs.items():
field = _GetFieldByName(message_descriptor, field_name)
if field is None:
raise TypeError("%s() got an unexpected keyword argument '%s'" %
@@ -527,7 +533,7 @@ def setter(self, new_value):
def _AddPropertiesForExtensions(descriptor, cls):
"""Adds properties for all fields in this protocol message type."""
extension_dict = descriptor.extensions_by_name
- for extension_name, extension_field in extension_dict.iteritems():
+ for extension_name, extension_field in extension_dict.items():
constant_name = extension_name.upper() + "_FIELD_NUMBER"
setattr(cls, constant_name, extension_field.number)
@@ -582,7 +588,7 @@ def _AddListFieldsMethod(message_descriptor, cls):
"""Helper for _AddMessageMethods()."""
def ListFields(self):
- all_fields = [item for item in self._fields.iteritems() if _IsPresent(item)]
+ all_fields = [item for item in iteritems(self._fields) if _IsPresent(item)]
all_fields.sort(key = lambda item: item[0].number)
return all_fields
@@ -697,8 +703,14 @@ def __eq__(self, other):
def _AddStrMethod(message_descriptor, cls):
"""Helper for _AddMessageMethods()."""
+
def __str__(self):
return text_format.MessageToString(self)
+
+ if sys.version_info[0] == 3:
+ def __str__(self, formatter=__str__):
+ return formatter(self).decode('utf-8')
+
cls.__str__ = __str__
@@ -864,7 +876,7 @@ def IsInitialized(self, errors=None):
errors.extend(self.FindInitializationErrors())
return False
- for field, value in self._fields.iteritems():
+ for field, value in iteritems(self._fields):
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
if field.label == _FieldDescriptor.LABEL_REPEATED:
for element in value:
@@ -933,7 +945,7 @@ def MergeFrom(self, msg):
fields = self._fields
- for field, value in msg._fields.iteritems():
+ for field, value in iteritems(msg._fields):
if field.label == LABEL_REPEATED:
field_value = fields.get(field)
if field_value is None:
View
90 python/google/protobuf/internal/reflection_test.py
@@ -128,10 +128,10 @@ def testRepeatedScalarConstructor(self):
repeated_bool=[True, False, False],
repeated_string=["optional_string"])
- self.assertEquals([1, 2, 3, 4], list(proto.repeated_int32))
- self.assertEquals([1.23, 54.321], list(proto.repeated_double))
- self.assertEquals([True, False, False], list(proto.repeated_bool))
- self.assertEquals(["optional_string"], list(proto.repeated_string))
+ self.assertEqual([1, 2, 3, 4], list(proto.repeated_int32))
+ self.assertEqual([1.23, 54.321], list(proto.repeated_double))
+ self.assertEqual([True, False, False], list(proto.repeated_bool))
+ self.assertEqual(["optional_string"], list(proto.repeated_string))
def testRepeatedCompositeConstructor(self):
# Constructor with only repeated composite types should succeed.
@@ -150,18 +150,18 @@ def testRepeatedCompositeConstructor(self):
unittest_pb2.TestAllTypes.RepeatedGroup(a=1),
unittest_pb2.TestAllTypes.RepeatedGroup(a=2)])
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.FOO),
unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.BAR)],
list(proto.repeated_nested_message))
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.ForeignMessage(c=-43),
unittest_pb2.ForeignMessage(c=45324),
unittest_pb2.ForeignMessage(c=12)],
list(proto.repeated_foreign_message))
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.TestAllTypes.RepeatedGroup(),
unittest_pb2.TestAllTypes.RepeatedGroup(a=1),
unittest_pb2.TestAllTypes.RepeatedGroup(a=2)],
@@ -186,15 +186,15 @@ def testMixedConstructor(self):
self.assertEqual(24, proto.optional_int32)
self.assertEqual('optional_string', proto.optional_string)
- self.assertEquals([1.23, 54.321], list(proto.repeated_double))
- self.assertEquals([True, False, False], list(proto.repeated_bool))
- self.assertEquals(
+ self.assertEqual([1.23, 54.321], list(proto.repeated_double))
+ self.assertEqual([True, False, False], list(proto.repeated_bool))
+ self.assertEqual(
[unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.FOO),
unittest_pb2.TestAllTypes.NestedMessage(
bb=unittest_pb2.TestAllTypes.BAR)],
list(proto.repeated_nested_message))
- self.assertEquals(
+ self.assertEqual(
[unittest_pb2.ForeignMessage(c=-43),
unittest_pb2.ForeignMessage(c=45324),
unittest_pb2.ForeignMessage(c=12)],
@@ -222,18 +222,18 @@ def testConstructorTypeError(self):
def testConstructorInvalidatesCachedByteSize(self):
message = unittest_pb2.TestAllTypes(optional_int32 = 12)
- self.assertEquals(2, message.ByteSize())
+ self.assertEqual(2, message.ByteSize())
message = unittest_pb2.TestAllTypes(
optional_nested_message = unittest_pb2.TestAllTypes.NestedMessage())
- self.assertEquals(3, message.ByteSize())
+ self.assertEqual(3, message.ByteSize())
message = unittest_pb2.TestAllTypes(repeated_int32 = [12])
- self.assertEquals(3, message.ByteSize())
+ self.assertEqual(3, message.ByteSize())
message = unittest_pb2.TestAllTypes(
repeated_nested_message = [unittest_pb2.TestAllTypes.NestedMessage()])
- self.assertEquals(3, message.ByteSize())
+ self.assertEqual(3, message.ByteSize())
def testSimpleHasBits(self):
# Test a scalar.
@@ -467,7 +467,7 @@ def testRepeatedListFields(self):
proto.repeated_string.extend(['foo', 'bar'])
proto.repeated_string.extend([])
proto.repeated_string.append('baz')
- proto.repeated_string.extend(str(x) for x in xrange(2))
+ proto.repeated_string.extend(str(x) for x in range(2))
proto.optional_int32 = 21
proto.repeated_bool # Access but don't set anything; should not be listed.
self.assertEqual(
@@ -533,7 +533,7 @@ def testDefaultValues(self):
self.assertEqual(0.0, proto.optional_double)
self.assertEqual(False, proto.optional_bool)
self.assertEqual('', proto.optional_string)
- self.assertEqual('', proto.optional_bytes)
+ self.assertEqual(b'', proto.optional_bytes)
self.assertEqual(41, proto.default_int32)
self.assertEqual(42, proto.default_int64)
@@ -549,7 +549,7 @@ def testDefaultValues(self):
self.assertEqual(52e3, proto.default_double)
self.assertEqual(True, proto.default_bool)
self.assertEqual('hello', proto.default_string)
- self.assertEqual('world', proto.default_bytes)
+ self.assertEqual(b'world', proto.default_bytes)
self.assertEqual(unittest_pb2.TestAllTypes.BAR, proto.default_nested_enum)
self.assertEqual(unittest_pb2.FOREIGN_BAR, proto.default_foreign_enum)
self.assertEqual(unittest_import_pb2.IMPORT_BAR,
@@ -791,7 +791,7 @@ def testRepeatedScalars(self):
self.assertEqual([5, 25, 20, 15, 30], proto.repeated_int32[:])
# Test slice assignment with an iterator
- proto.repeated_int32[1:4] = (i for i in xrange(3))
+ proto.repeated_int32[1:4] = (i for i in range(3))
self.assertEqual([5, 0, 1, 2, 30], proto.repeated_int32)
# Test slice assignment.
@@ -994,6 +994,10 @@ def testHandWrittenReflection(self):
containing_type=None, nested_types=[], enum_types=[],
fields=[foo_field_descriptor], extensions=[],
options=descriptor_pb2.MessageOptions())
+
+ from google.protobuf.metaclass import decorator
+
+ @decorator
class MyProtoClass(message.Message):
DESCRIPTOR = mydescriptor
__metaclass__ = reflection.GeneratedProtocolMessageType
@@ -1036,12 +1040,14 @@ def AddDescriptorField(proto, field_name, field_type):
new_field.label = descriptor_pb2.FieldDescriptorProto.LABEL_REPEATED
desc = descriptor.MakeDescriptor(desc_proto)
- self.assertTrue(desc.fields_by_name.has_key('name'))
- self.assertTrue(desc.fields_by_name.has_key('year'))
- self.assertTrue(desc.fields_by_name.has_key('automatic'))
- self.assertTrue(desc.fields_by_name.has_key('price'))
- self.assertTrue(desc.fields_by_name.has_key('owners'))
-
+ self.assertTrue('name' in desc.fields_by_name)
+ self.assertTrue('year' in desc.fields_by_name)
+ self.assertTrue('automatic' in desc.fields_by_name)
+ self.assertTrue('price' in desc.fields_by_name)
+ self.assertTrue('owners' in desc.fields_by_name)
+
+ from google.protobuf.metaclass import decorator
+ @decorator
class CarMessage(message.Message):
__metaclass__ = reflection.GeneratedProtocolMessageType
DESCRIPTOR = desc
@@ -1461,18 +1467,18 @@ def testClear(self):
test_util.SetAllNonLazyFields(proto)
# Clear the message.
proto.Clear()
- self.assertEquals(proto.ByteSize(), 0)
+ self.assertEqual(proto.ByteSize(), 0)
empty_proto = unittest_pb2.TestAllTypes()
- self.assertEquals(proto, empty_proto)
+ self.assertEqual(proto, empty_proto)
# Test if extensions which were set are cleared.
proto = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(proto)
# Clear the message.
proto.Clear()
- self.assertEquals(proto.ByteSize(), 0)
+ self.assertEqual(proto.ByteSize(), 0)
empty_proto = unittest_pb2.TestAllExtensions()
- self.assertEquals(proto, empty_proto)
+ self.assertEqual(proto, empty_proto)
def testDisconnectingBeforeClear(self):
proto = unittest_pb2.TestAllTypes()
@@ -1579,26 +1585,26 @@ def testStringUTF8Encoding(self):
setattr, proto, 'optional_bytes', u'unicode object')
# Check that the default value is of python's 'unicode' type.
- self.assertEqual(type(proto.optional_string), unicode)
+ self.assertEqual(type(proto.optional_string), type(u''))
- proto.optional_string = unicode('Testing')
- self.assertEqual(proto.optional_string, str('Testing'))
+ proto.optional_string = u'Testing'
+ self.assertEqual(proto.optional_string, 'Testing')
# Assign a value of type 'str' which can be encoded in UTF-8.
proto.optional_string = str('Testing')
- self.assertEqual(proto.optional_string, unicode('Testing'))
+ self.assertEqual(proto.optional_string, u'Testing')
if api_implementation.Type() == 'python':
# Values of type 'str' are also accepted as long as they can be
# encoded in UTF-8.
- self.assertEqual(type(proto.optional_string), str)
+ self.assertEqual(type(proto.optional_string), type(''))
# Try to assign a 'str' value which contains bytes that aren't 7-bit ASCII.
self.assertRaises(ValueError,
- setattr, proto, 'optional_string', str('a\x80a'))
+ setattr, proto, 'optional_string', b'a\x80a')
# Assign a 'str' object which contains a UTF-8 encoded string.
self.assertRaises(ValueError,
- setattr, proto, 'optional_string', 'Тест')
+ setattr, proto, 'optional_string', u'Тест'.encode('utf-8'))
# No exception thrown.
proto.optional_string = 'abc'
@@ -1634,7 +1640,7 @@ def testStringUTF8Serialization(self):
raw.item[0].message.endswith(test_utf8_bytes))
message2.MergeFromString(raw.item[0].message)
- self.assertEqual(type(message2.str), unicode)
+ self.assertEqual(type(message2.str), type(u''))
self.assertEqual(message2.str, test_utf8)
# The pure Python API throws an exception on MergeFromString(),
@@ -1645,7 +1651,7 @@ def testStringUTF8Serialization(self):
# The pure Python API always returns objects of type 'unicode' (UTF-8
# encoded), or 'str' (in 7 bit ASCII).
bytes = raw.item[0].message.replace(
- test_utf8_bytes, len(test_utf8_bytes) * '\xff')
+ test_utf8_bytes, len(test_utf8_bytes) * b'\xff')
unicode_decode_failed = False
try:
@@ -2184,7 +2190,7 @@ def testParseTruncated(self):
test_util.SetAllFields(first_proto)
serialized = first_proto.SerializeToString()
- for truncation_point in xrange(len(serialized) + 1):
+ for truncation_point in range(len(serialized) + 1):
try:
second_proto = unittest_pb2.TestAllTypes()
unknown_fields = unittest_pb2.TestEmptyMessage()
@@ -2332,7 +2338,7 @@ def testMessageSetWireFormatUnknownExtension(self):
# Check that the message parsed well.
extension_message1 = unittest_mset_pb2.TestMessageSetExtension1
extension1 = extension_message1.message_set_extension
- self.assertEquals(12345, proto.Extensions[extension1].i)
+ self.assertEqual(12345, proto.Extensions[extension1].i)
def testUnknownFields(self):
proto = unittest_pb2.TestAllTypes()
@@ -2572,7 +2578,7 @@ def testInitKwargs(self):
optional_int32=1,
optional_string='foo',
optional_bool=True,
- optional_bytes='bar',
+ optional_bytes=b'bar',
optional_nested_message=unittest_pb2.TestAllTypes.NestedMessage(bb=1),
optional_foreign_message=unittest_pb2.ForeignMessage(c=1),
optional_nested_enum=unittest_pb2.TestAllTypes.FOO,
@@ -2590,7 +2596,7 @@ def testInitKwargs(self):
self.assertEqual(1, proto.optional_int32)
self.assertEqual('foo', proto.optional_string)
self.assertEqual(True, proto.optional_bool)
- self.assertEqual('bar', proto.optional_bytes)
+ self.assertEqual(b'bar', proto.optional_bytes)
self.assertEqual(1, proto.optional_nested_message.bb)
self.assertEqual(1, proto.optional_foreign_message.c)
self.assertEqual(unittest_pb2.TestAllTypes.FOO,
View
26 python/google/protobuf/internal/test_util.py
@@ -73,7 +73,7 @@ def SetAllNonLazyFields(message):
# to finish with Kenton about bytes vs. strings
# and forcing everything to be utf8. :-/
message.optional_string = '115'
- message.optional_bytes = '116'
+ message.optional_bytes = b'116'
message.optionalgroup.a = 117
message.optional_nested_message.bb = 118
@@ -106,7 +106,7 @@ def SetAllNonLazyFields(message):
message.repeated_double.append(212)
message.repeated_bool.append(True)
message.repeated_string.append('215')
- message.repeated_bytes.append('216')
+ message.repeated_bytes.append(b'216')
message.repeatedgroup.add().a = 217
message.repeated_nested_message.add().bb = 218
@@ -136,7 +136,7 @@ def SetAllNonLazyFields(message):
message.repeated_double.append(312)
message.repeated_bool.append(False)
message.repeated_string.append('315')
- message.repeated_bytes.append('316')
+ message.repeated_bytes.append(b'316')
message.repeatedgroup.add().a = 317
message.repeated_nested_message.add().bb = 318
@@ -169,7 +169,7 @@ def SetAllNonLazyFields(message):
message.default_double = 412
message.default_bool = False
message.default_string = '415'
- message.default_bytes = '416'
+ message.default_bytes = b'416'
message.default_nested_enum = unittest_pb2.TestAllTypes.FOO
message.default_foreign_enum = unittest_pb2.FOREIGN_FOO
@@ -213,7 +213,7 @@ def SetAllExtensions(message):
extensions[pb2.optional_double_extension] = 112
extensions[pb2.optional_bool_extension] = True
extensions[pb2.optional_string_extension] = '115'
- extensions[pb2.optional_bytes_extension] = '116'
+ extensions[pb2.optional_bytes_extension] = b'116'
extensions[pb2.optionalgroup_extension].a = 117
extensions[pb2.optional_nested_message_extension].bb = 118
@@ -248,7 +248,7 @@ def SetAllExtensions(message):
extensions[pb2.repeated_double_extension].append(212)
extensions[pb2.repeated_bool_extension].append(True)
extensions[pb2.repeated_string_extension].append('215')
- extensions[pb2.repeated_bytes_extension].append('216')
+ extensions[pb2.repeated_bytes_extension].append(b'216')
extensions[pb2.repeatedgroup_extension].add().a = 217
extensions[pb2.repeated_nested_message_extension].add().bb = 218
@@ -278,7 +278,7 @@ def SetAllExtensions(message):
extensions[pb2.repeated_double_extension].append(312)
extensions[pb2.repeated_bool_extension].append(False)
extensions[pb2.repeated_string_extension].append('315')
- extensions[pb2.repeated_bytes_extension].append('316')
+ extensions[pb2.repeated_bytes_extension].append(b'316')
extensions[pb2.repeatedgroup_extension].add().a = 317
extensions[pb2.repeated_nested_message_extension].add().bb = 318
@@ -311,7 +311,7 @@ def SetAllExtensions(message):
extensions[pb2.default_double_extension] = 412
extensions[pb2.default_bool_extension] = False
extensions[pb2.default_string_extension] = '415'
- extensions[pb2.default_bytes_extension] = '416'
+ extensions[pb2.default_bytes_extension] = b'416'
extensions[pb2.default_nested_enum_extension] = pb2.TestAllTypes.FOO
extensions[pb2.default_foreign_enum_extension] = pb2.FOREIGN_FOO
@@ -358,7 +358,7 @@ def ExpectAllFieldsAndExtensionsInOrder(serialized):
message.my_float = 1.0
expected_strings.append(message.SerializeToString())
message.Clear()
- expected = ''.join(expected_strings)
+ expected = b''.join(expected_strings)
if expected != serialized:
raise ValueError('Expected %r, found %r' % (expected, serialized))
@@ -413,7 +413,7 @@ def ExpectAllFieldsSet(test_case, message):
test_case.assertEqual(112, message.optional_double)
test_case.assertEqual(True, message.optional_bool)
test_case.assertEqual('115', message.optional_string)
- test_case.assertEqual('116', message.optional_bytes)
+ test_case.assertEqual(b'116', message.optional_bytes)
test_case.assertEqual(117, message.optionalgroup.a)
test_case.assertEqual(118, message.optional_nested_message.bb)
@@ -472,7 +472,7 @@ def ExpectAllFieldsSet(test_case, message):
test_case.assertEqual(212, message.repeated_double[0])
test_case.assertEqual(True, message.repeated_bool[0])
test_case.assertEqual('215', message.repeated_string[0])
- test_case.assertEqual('216', message.repeated_bytes[0])
+ test_case.assertEqual(b'216', message.repeated_bytes[0])
test_case.assertEqual(217, message.repeatedgroup[0].a)
test_case.assertEqual(218, message.repeated_nested_message[0].bb)
@@ -501,7 +501,7 @@ def ExpectAllFieldsSet(test_case, message):
test_case.assertEqual(312, message.repeated_double[1])
test_case.assertEqual(False, message.repeated_bool[1])
test_case.assertEqual('315', message.repeated_string[1])
- test_case.assertEqual('316', message.repeated_bytes[1])
+ test_case.assertEqual(b'316', message.repeated_bytes[1])
test_case.assertEqual(317, message.repeatedgroup[1].a)
test_case.assertEqual(318, message.repeated_nested_message[1].bb)
@@ -552,7 +552,7 @@ def ExpectAllFieldsSet(test_case, message):
test_case.assertEqual(412, message.default_double)
test_case.assertEqual(False, message.default_bool)
test_case.assertEqual('415', message.default_string)
- test_case.assertEqual('416', message.default_bytes)
+ test_case.assertEqual(b'416', message.default_bytes)
test_case.assertEqual(unittest_pb2.TestAllTypes.FOO,
message.default_nested_enum)
View
305 python/google/protobuf/internal/text_format_test.py
@@ -85,14 +85,14 @@ def testPrintMessageSet(self):
message.message_set.Extensions[ext1].i = 23
message.message_set.Extensions[ext2].str = 'foo'
self.CompareToGoldenText(text_format.MessageToString(message),
- 'message_set {\n'
- ' [protobuf_unittest.TestMessageSetExtension1] {\n'
- ' i: 23\n'
- ' }\n'
- ' [protobuf_unittest.TestMessageSetExtension2] {\n'
- ' str: \"foo\"\n'
- ' }\n'
- '}\n')
+ b'message_set {\n'
+ b' [protobuf_unittest.TestMessageSetExtension1] {\n'
+ b' i: 23\n'
+ b' }\n'
+ b' [protobuf_unittest.TestMessageSetExtension2] {\n'
+ b' str: \"foo\"\n'
+ b' }\n'
+ b'}\n')
def testPrintBadEnumValue(self):
message = unittest_pb2.TestAllTypes()
@@ -101,9 +101,9 @@ def testPrintBadEnumValue(self):
message.optional_import_enum = 102
self.CompareToGoldenText(
text_format.MessageToString(message),
- 'optional_nested_enum: 100\n'
- 'optional_foreign_enum: 101\n'
- 'optional_import_enum: 102\n')
+ b'optional_nested_enum: 100\n'
+ b'optional_foreign_enum: 101\n'
+ b'optional_import_enum: 102\n')
def testPrintBadEnumValueExtensions(self):
message = unittest_pb2.TestAllExtensions()
@@ -112,9 +112,9 @@ def testPrintBadEnumValueExtensions(self):
message.Extensions[unittest_pb2.optional_import_enum_extension] = 102
self.CompareToGoldenText(
text_format.MessageToString(message),
- '[protobuf_unittest.optional_nested_enum_extension]: 100\n'
- '[protobuf_unittest.optional_foreign_enum_extension]: 101\n'
- '[protobuf_unittest.optional_import_enum_extension]: 102\n')
+ b'[protobuf_unittest.optional_nested_enum_extension]: 100\n'
+ b'[protobuf_unittest.optional_foreign_enum_extension]: 101\n'
+ b'[protobuf_unittest.optional_import_enum_extension]: 102\n')
def testPrintExotic(self):
message = unittest_pb2.TestAllTypes()
@@ -127,22 +127,22 @@ def testPrintExotic(self):
message.repeated_string.append(u'\u00fc\ua71f')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(message)),
- 'repeated_int64: -9223372036854775808\n'
- 'repeated_uint64: 18446744073709551615\n'
- 'repeated_double: 123.456\n'
- 'repeated_double: 1.23e+22\n'
- 'repeated_double: 1.23e-18\n'
- 'repeated_string: '
- '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
- 'repeated_string: "\\303\\274\\352\\234\\237"\n')
+ b'repeated_int64: -9223372036854775808\n'
+ b'repeated_uint64: 18446744073709551615\n'
+ b'repeated_double: 123.456\n'
+ b'repeated_double: 1.23e+22\n'
+ b'repeated_double: 1.23e-18\n'
+ b'repeated_string: '
+ b'"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
+ b'repeated_string: "\\303\\274\\352\\234\\237"\n')
def testPrintNestedMessageAsOneLine(self):
message = unittest_pb2.TestAllTypes()
msg = message.repeated_nested_message.add()
msg.bb = 42;
self.CompareToGoldenText(
text_format.MessageToString(message, as_one_line=True),
- 'repeated_nested_message { bb: 42 }')
+ b'repeated_nested_message { bb: 42 }')
def testPrintRepeatedFieldsAsOneLine(self):
message = unittest_pb2.TestAllTypes()
@@ -153,15 +153,15 @@ def testPrintRepeatedFieldsAsOneLine(self):
message.repeated_string.append("Zurich")
self.CompareToGoldenText(
text_format.MessageToString(message, as_one_line=True),
- 'repeated_int32: 1 repeated_int32: 1 repeated_int32: 3 '
- 'repeated_string: "Google" repeated_string: "Zurich"')
+ b'repeated_int32: 1 repeated_int32: 1 repeated_int32: 3 '
+ b'repeated_string: "Google" repeated_string: "Zurich"')
def testPrintNestedNewLineInStringAsOneLine(self):
message = unittest_pb2.TestAllTypes()
message.optional_string = "a\nnew\nline"
self.CompareToGoldenText(
text_format.MessageToString(message, as_one_line=True),
- 'optional_string: "a\\nnew\\nline"')
+ b'optional_string: "a\\nnew\\nline"')
def testPrintMessageSetAsOneLine(self):
message = unittest_mset_pb2.TestMessageSetContainer()
@@ -171,14 +171,14 @@ def testPrintMessageSetAsOneLine(self):
message.message_set.Extensions[ext2].str = 'foo'
self.CompareToGoldenText(
text_format.MessageToString(message, as_one_line=True),
- 'message_set {'
- ' [protobuf_unittest.TestMessageSetExtension1] {'
- ' i: 23'
- ' }'
- ' [protobuf_unittest.TestMessageSetExtension2] {'
- ' str: \"foo\"'
- ' }'
- ' }')
+ b'message_set {'
+ b' [protobuf_unittest.TestMessageSetExtension1] {'
+ b' i: 23'
+ b' }'
+ b' [protobuf_unittest.TestMessageSetExtension2] {'
+ b' str: \"foo\"'
+ b' }'
+ b' }')
def testPrintExoticAsOneLine(self):
message = unittest_pb2.TestAllTypes()
@@ -192,14 +192,14 @@ def testPrintExoticAsOneLine(self):
self.CompareToGoldenText(
self.RemoveRedundantZeros(
text_format.MessageToString(message, as_one_line=True)),
- 'repeated_int64: -9223372036854775808'
- ' repeated_uint64: 18446744073709551615'
- ' repeated_double: 123.456'
- ' repeated_double: 1.23e+22'
- ' repeated_double: 1.23e-18'
- ' repeated_string: '
- '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""'
- ' repeated_string: "\\303\\274\\352\\234\\237"')
+ b'repeated_int64: -9223372036854775808'
+ b' repeated_uint64: 18446744073709551615'
+ b' repeated_double: 123.456'
+ b' repeated_double: 1.23e+22'
+ b' repeated_double: 1.23e-18'
+ b' repeated_string: '
+ b'"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""'
+ b' repeated_string: "\\303\\274\\352\\234\\237"')
def testRoundTripExoticAsOneLine(self):
message = unittest_pb2.TestAllTypes()
@@ -216,23 +216,23 @@ def testRoundTripExoticAsOneLine(self):
message, as_one_line=True, as_utf8=False)
parsed_message = unittest_pb2.TestAllTypes()
text_format.Merge(wire_text, parsed_message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
# Test as_utf8 = True.
wire_text = text_format.MessageToString(
message, as_one_line=True, as_utf8=True)
parsed_message = unittest_pb2.TestAllTypes()
text_format.Merge(wire_text, parsed_message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testPrintRawUtf8String(self):
message = unittest_pb2.TestAllTypes()
message.repeated_string.append(u'\u00fc\ua71f')
text = text_format.MessageToString(message, as_utf8 = True)
- self.CompareToGoldenText(text, 'repeated_string: "\303\274\352\234\237"\n')
+ self.CompareToGoldenText(text, b'repeated_string: "\303\274\352\234\237"\n')
parsed_message = unittest_pb2.TestAllTypes()
text_format.Merge(text, parsed_message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testMessageToString(self):
message = unittest_pb2.ForeignMessage()
@@ -242,31 +242,31 @@ def testMessageToString(self):
def RemoveRedundantZeros(self, text):
# Some platforms print 1e+5 as 1e+005. This is fine, but we need to remove
# these zeros in order to match the golden file.
- text = text.replace('e+0','e+').replace('e+0','e+') \
- .replace('e-0','e-').replace('e-0','e-')
+ text = text.replace(b'e+0', b'e+').replace(b'e+0', b'e+') \
+ .replace(b'e-0', b'e-').replace(b'e-0', b'e-')
# Floating point fields are printed with .0 suffix even if they are
# actualy integer numbers.
- text = re.compile('\.0$', re.MULTILINE).sub('', text)
+ text = re.compile(b'\.0$', re.MULTILINE).sub(b'', text)
return text
def testMergeGolden(self):
- golden_text = '\n'.join(self.ReadGolden('text_format_unittest_data.txt'))
+ golden_text = b'\n'.join(self.ReadGolden('text_format_unittest_data.txt'))
parsed_message = unittest_pb2.TestAllTypes()
text_format.Merge(golden_text, parsed_message)
message = unittest_pb2.TestAllTypes()
test_util.SetAllFields(message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testMergeGoldenExtensions(self):
- golden_text = '\n'.join(self.ReadGolden(
+ golden_text = b'\n'.join(self.ReadGolden(
'text_format_unittest_extensions_data.txt'))
parsed_message = unittest_pb2.TestAllExtensions()
text_format.Merge(golden_text, parsed_message)
message = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(message)
- self.assertEquals(message, parsed_message)
+ self.assertEqual(message, parsed_message)
def testMergeAllFields(self):
message = unittest_pb2.TestAllTypes()
@@ -282,47 +282,46 @@ def testMergeAllExtensions(self):
message = unittest_pb2.TestAllExtensions()
test_util.SetAllExtensions(message)
ascii_text = text_format.MessageToString(message)
-
parsed_message = unittest_pb2.TestAllExtensions()
text_format.Merge(ascii_text, parsed_message)
self.assertEqual(message, parsed_message)
def testMergeMessageSet(self):
message = unittest_pb2.TestAllTypes()
- text = ('repeated_uint64: 1\n'
- 'repeated_uint64: 2\n')
+ text = (b'repeated_uint64: 1\n'
+ b'repeated_uint64: 2\n')
text_format.Merge(text, message)
self.assertEqual(1, message.repeated_uint64[0])
self.assertEqual(2, message.repeated_uint64[1])
message = unittest_mset_pb2.TestMessageSetContainer()
- text = ('message_set {\n'
- ' [protobuf_unittest.TestMessageSetExtension1] {\n'
- ' i: 23\n'
- ' }\n'
- ' [protobuf_unittest.TestMessageSetExtension2] {\n'
- ' str: \"foo\"\n'
- ' }\n'
- '}\n')
+ text = (b'message_set {\n'
+ b' [protobuf_unittest.TestMessageSetExtension1] {\n'
+ b' i: 23\n'
+ b' }\n'
+ b' [protobuf_unittest.TestMessageSetExtension2] {\n'
+ b' str: \"foo\"\n'
+ b' }\n'
+ b'}\n')
text_format.Merge(text, message)
ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
- self.assertEquals(23, message.message_set.Extensions[ext1].i)
- self.assertEquals('foo', message.message_set.Extensions[ext2].str)
+ self.assertEqual(23, message.message_set.Extensions[ext1].i)
+ self.assertEqual('foo', message.message_set.Extensions[ext2].str)
def testMergeExotic(self):
message = unittest_pb2.TestAllTypes()
- text = ('repeated_int64: -9223372036854775808\n'
- 'repeated_uint64: 18446744073709551615\n'
- 'repeated_double: 123.456\n'
- 'repeated_double: 1.23e+22\n'
- 'repeated_double: 1.23e-18\n'
- 'repeated_string: \n'
- '"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
- 'repeated_string: "foo" \'corge\' "grault"\n'
- 'repeated_string: "\\303\\274\\352\\234\\237"\n'
- 'repeated_string: "\\xc3\\xbc"\n'
- 'repeated_string: "\xc3\xbc"\n')
+ text = (b'repeated_int64: -9223372036854775808\n'
+ b'repeated_uint64: 18446744073709551615\n'
+ b'repeated_double: 123.456\n'
+ b'repeated_double: 1.23e+22\n'
+ b'repeated_double: 1.23e-18\n'
+ b'repeated_string: \n'
+ b'"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
+ b'repeated_string: "foo" \'corge\' "grault"\n'
+ b'repeated_string: "\\303\\274\\352\\234\\237"\n'
+ b'repeated_string: "\\xc3\\xbc"\n'
+ b'repeated_string: "\xc3\xbc"\n')
text_format.Merge(text, message)
self.assertEqual(-9223372036854775808, message.repeated_int64[0])
@@ -338,18 +337,18 @@ def testMergeExotic(self):
def testMergeEmptyText(self):
message = unittest_pb2.TestAllTypes()
- text = ''
+ text = b''
text_format.Merge(text, message)
- self.assertEquals(unittest_pb2.TestAllTypes(), message)
+ self.assertEqual(unittest_pb2.TestAllTypes(), message)
def testMergeInvalidUtf8(self):
message = unittest_pb2.TestAllTypes()
- text = 'repeated_string: "\\xc3\\xc3"'
+ text = b'repeated_string: "\\xc3\\xc3"'
self.assertRaises(text_format.ParseError, text_format.Merge, text, message)
def testMergeSingleWord(self):
message = unittest_pb2.TestAllTypes()
- text = 'foo'
+ text = b'foo'
self.assertRaisesWithMessage(
text_format.ParseError,
('1:1 : Message type "protobuf_unittest.TestAllTypes" has no field named '
@@ -358,7 +357,7 @@ def testMergeSingleWord(self):
def testMergeUnknownField(self):
message = unittest_pb2.TestAllTypes()
- text = 'unknown_field: 8\n'
+ text = b'unknown_field: 8\n'
self.assertRaisesWithMessage(
text_format.ParseError,
('1:1 : Message type "protobuf_unittest.TestAllTypes" has no field named '
@@ -367,7 +366,7 @@ def testMergeUnknownField(self):
def testMergeBadExtension(self):
message = unittest_pb2.TestAllExtensions()
- text = '[unknown_extension]: 8\n'
+ text = b'[unknown_extension]: 8\n'
self.assertRaisesWithMessage(
text_format.ParseError,
'1:2 : Extension "unknown_extension" not registered.',
@@ -381,40 +380,40 @@ def testMergeBadExtension(self):
def testMergeGroupNotClosed(self):
message = unittest_pb2.TestAllTypes()
- text = 'RepeatedGroup: <'
+ text = b'RepeatedGroup: <'
self.assertRaisesWithMessage(
text_format.ParseError, '1:16 : Expected ">".',
text_format.Merge, text, message)
- text = 'RepeatedGroup: {'
+ text = b'RepeatedGroup: {'
self.assertRaisesWithMessage(
text_format.ParseError, '1:16 : Expected "}".',
text_format.Merge, text, message)
def testMergeEmptyGroup(self):
message = unittest_pb2.TestAllTypes()
- text = 'OptionalGroup: {}'
+ text = b'OptionalGroup: {}'
text_format.Merge(text, message)
self.assertTrue(message.HasField('optionalgroup'))
message.Clear()
message = unittest_pb2.TestAllTypes()
- text = 'OptionalGroup: <>'
+ text = b'OptionalGroup: <>'
text_format.Merge(text, message)
self.assertTrue(message.HasField('optionalgroup'))
def testMergeBadEnumValue(self):
message = unittest_pb2.TestAllTypes()
- text = 'optional_nested_enum: BARR'
+ text = b'optional_nested_enum: BARR'
self.assertRaisesWithMessage(
text_format.ParseError,
('1:23 : Enum type "protobuf_unittest.TestAllTypes.NestedEnum" '
'has no value named BARR.'),
text_format.Merge, text, message)
message = unittest_pb2.TestAllTypes()
- text = 'optional_nested_enum: 100'
+ text = b'optional_nested_enum: 100'
self.assertRaisesWithMessage(
text_format.ParseError,
('1:23 : Enum type "protobuf_unittest.TestAllTypes.NestedEnum" '
@@ -423,7 +422,7 @@ def testMergeBadEnumValue(self):
def testMergeBadIntValue(self):
message = unittest_pb2.TestAllTypes()
- text = 'optional_int32: bork'
+ text = b'optional_int32: bork'
self.assertRaisesWithMessage(
text_format.ParseError,
('1:17 : Couldn\'t parse integer: bork'),
@@ -436,7 +435,7 @@ def testMergeStringFieldUnescape(self):
repeated_string: "\\\xf\\\x62"
repeated_string: "\\\\xf\\\\x62"
repeated_string: "\\\\\xf\\\\\x62"
- repeated_string: "\x5cx20"'''
+ repeated_string: "\x5cx20"'''.encode('ascii')
text_format.Merge(text, message)
SLASH = '\\'
@@ -472,86 +471,86 @@ def assertRaisesWithMessage(self, e_class, e, func, *args, **kwargs):
class TokenizerTest(unittest.TestCase):
def testSimpleTokenCases(self):
- text = ('identifier1:"string1"\n \n\n'
- 'identifier2 : \n \n123 \n identifier3 :\'string\'\n'
- 'identifiER_4 : 1.1e+2 ID5:-0.23 ID6:\'aaaa\\\'bbbb\'\n'
- 'ID7 : "aa\\"bb"\n\n\n\n ID8: {A:inf B:-inf C:true D:false}\n'
- 'ID9: 22 ID10: -111111111111111111 ID11: -22\n'
- 'ID12: 2222222222222222222 ID13: 1.23456f ID14: 1.2e+2f '
- 'false_bool: 0 true_BOOL:t \n true_bool1: 1 false_BOOL1:f ' )
+ text = (b'identifier1:"string1"\n \n\n'
+ b'identifier2 : \n \n123 \n identifier3 :\'string\'\n'
+ b'identifiER_4 : 1.1e+2 ID5:-0.23 ID6:\'aaaa\\\'bbbb\'\n'
+ b'ID7 : "aa\\"bb"\n\n\n\n ID8: {A:inf B:-inf C:true D:false}\n'
+ b'ID9: 22 ID10: -111111111111111111 ID11: -22\n'
+ b'ID12: 2222222222222222222 ID13: 1.23456f ID14: 1.2e+2f '
+ b'false_bool: 0 true_BOOL:t \n true_bool1: 1 false_BOOL1:f ' )
tokenizer = text_format._Tokenizer(text)
- methods = [(tokenizer.ConsumeIdentifier, 'identifier1'),
- ':',
+ methods = [(tokenizer.ConsumeIdentifier, b'identifier1'),
+ b':',
(tokenizer.ConsumeString, 'string1'),
- (tokenizer.ConsumeIdentifier, 'identifier2'),
- ':',
+ (tokenizer.ConsumeIdentifier, b'identifier2'),
+ b':',
(tokenizer.ConsumeInt32, 123),
- (tokenizer.ConsumeIdentifier, 'identifier3'),
- ':',
+ (tokenizer.ConsumeIdentifier, b'identifier3'),
+ b':',
(tokenizer.ConsumeString, 'string'),
- (tokenizer.ConsumeIdentifier, 'identifiER_4'),
- ':',
+ (tokenizer.ConsumeIdentifier, b'identifiER_4'),
+ b':',
(tokenizer.ConsumeFloat, 1.1e+2),
- (tokenizer.ConsumeIdentifier, 'ID5'),
- ':',
+