Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/unknown_fields.py: 7%

54 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:37 +0000

1# Protocol Buffers - Google's data interchange format 

2# Copyright 2008 Google Inc. All rights reserved. 

3# https://developers.google.com/protocol-buffers/ 

4# 

5# Redistribution and use in source and binary forms, with or without 

6# modification, are permitted provided that the following conditions are 

7# met: 

8# 

9# * Redistributions of source code must retain the above copyright 

10# notice, this list of conditions and the following disclaimer. 

11# * Redistributions in binary form must reproduce the above 

12# copyright notice, this list of conditions and the following disclaimer 

13# in the documentation and/or other materials provided with the 

14# distribution. 

15# * Neither the name of Google Inc. nor the names of its 

16# contributors may be used to endorse or promote products derived from 

17# this software without specific prior written permission. 

18# 

19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 

20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 

21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 

22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 

23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 

24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 

25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 

26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 

27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 

28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 

29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

30 

31"""Contains Unknown Fields APIs. 

32 

33Simple usage example: 

34 unknown_field_set = UnknownFieldSet(message) 

35 for unknown_field in unknown_field_set: 

36 wire_type = unknown_field.wire_type 

37 field_number = unknown_field.field_number 

38 data = unknown_field.data 

39""" 

40 

41 

42from google.protobuf.internal import api_implementation 

43 

44if api_implementation._c_module is not None: # pylint: disable=protected-access 

45 UnknownFieldSet = api_implementation._c_module.UnknownFieldSet # pylint: disable=protected-access 

46else: 

47 from google.protobuf.internal import decoder # pylint: disable=g-import-not-at-top 

48 from google.protobuf.internal import wire_format # pylint: disable=g-import-not-at-top 

49 

50 class UnknownField: 

51 """A parsed unknown field.""" 

52 

53 # Disallows assignment to other attributes. 

54 __slots__ = ['_field_number', '_wire_type', '_data'] 

55 

56 def __init__(self, field_number, wire_type, data): 

57 self._field_number = field_number 

58 self._wire_type = wire_type 

59 self._data = data 

60 return 

61 

62 @property 

63 def field_number(self): 

64 return self._field_number 

65 

66 @property 

67 def wire_type(self): 

68 return self._wire_type 

69 

70 @property 

71 def data(self): 

72 return self._data 

73 

74 class UnknownFieldSet: 

75 """UnknownField container.""" 

76 

77 # Disallows assignment to other attributes. 

78 __slots__ = ['_values'] 

79 

80 def __init__(self, msg): 

81 

82 def InternalAdd(field_number, wire_type, data): 

83 unknown_field = UnknownField(field_number, wire_type, data) 

84 self._values.append(unknown_field) 

85 

86 self._values = [] 

87 msg_des = msg.DESCRIPTOR 

88 # pylint: disable=protected-access 

89 unknown_fields = msg._unknown_fields 

90 if (msg_des.has_options and 

91 msg_des.GetOptions().message_set_wire_format): 

92 local_decoder = decoder.UnknownMessageSetItemDecoder() 

93 for _, buffer in unknown_fields: 

94 (field_number, data) = local_decoder(memoryview(buffer)) 

95 InternalAdd(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED, data) 

96 else: 

97 for tag_bytes, buffer in unknown_fields: 

98 # pylint: disable=protected-access 

99 (tag, _) = decoder._DecodeVarint(tag_bytes, 0) 

100 field_number, wire_type = wire_format.UnpackTag(tag) 

101 if field_number == 0: 

102 raise RuntimeError('Field number 0 is illegal.') 

103 (data, _) = decoder._DecodeUnknownField( 

104 memoryview(buffer), 0, wire_type) 

105 InternalAdd(field_number, wire_type, data) 

106 

107 def __getitem__(self, index): 

108 size = len(self._values) 

109 if index < 0: 

110 index += size 

111 if index < 0 or index >= size: 

112 raise IndexError('index %d out of range'.index) 

113 

114 return self._values[index] 

115 

116 def __len__(self): 

117 return len(self._values) 

118 

119 def __iter__(self): 

120 return iter(self._values)