def MergeFromString(self, serialized): length = len(serialized) try: if self._InternalParse(serialized, 0, length) != length: raise message_mod.DecodeError('Unexpected end-group tag.') except IndexError: raise message_mod.DecodeError('Truncated message.') except struct.error, e: raise message_mod.DecodeError(e)
def MergeFromString(self, serialized): if isinstance(serialized, memoryview) and six.PY2: raise TypeError( 'memoryview not supported in Python 2 with the pure Python proto ' 'implementation: this is to maintain compatibility with the C++ ' 'implementation') serialized = memoryview(serialized) length = len(serialized) try: if self._InternalParse(serialized, 0, length) != length: raise message_mod.DecodeError('Unexpected end-group tag.') except (IndexError, TypeError): raise message_mod.DecodeError('Truncated message.') except struct.error as e: raise message_mod.DecodeError(e) return length
def _ConvertToUnicode(memview): """Convert byte to unicode.""" byte_str = memview.tobytes() try: value = local_unicode(byte_str, 'utf-8') except UnicodeDecodeError as e: e.reason = '%s in field: %s' % (e, key.full_name) raise if is_strict_utf8 and six.PY2 and sys.maxunicode > _UCS2_MAXUNICODE: if _SURROGATE_PATTERN.search(value): reason = ('String field %s contains invalid UTF-8 data when parsing' 'a protocol buffer: surrogates not allowed. Use' 'the bytes type if you intend to send raw bytes.') % ( key.full_name) raise message.DecodeError(reason) return value