Go to the documentation of this file.
35 """Contains an abstract base class for protocol messages."""
37 __author__ =
'robinson@google.com (Will Robinson)'
39 class Error(Exception):
40 """Base error type for this module."""
44 class DecodeError(Error):
45 """Exception raised when deserializing messages."""
49 class EncodeError(Error):
50 """Exception raised when serializing messages."""
56 """Abstract base class for protocol messages.
58 Protocol message classes are almost always generated by the protocol
59 compiler. These generated types subclass Message and implement the methods
86 """Recursively compares two messages by value and structure."""
87 raise NotImplementedError
91 return not self == other_msg
94 raise TypeError(
'unhashable object')
97 """Outputs a human-readable representation of the message."""
98 raise NotImplementedError
101 """Outputs a human-readable representation of the message."""
102 raise NotImplementedError
105 """Merges the contents of the specified message into current message.
107 This method merges the contents of the specified message into the current
108 message. Singular fields that are set in the specified message overwrite
109 the corresponding fields in the current message. Repeated fields are
110 appended. Singular sub-messages and groups are recursively merged.
113 other_msg (Message): A message to merge into the current message.
115 raise NotImplementedError
118 """Copies the content of the specified message into the current message.
120 The method clears the current message and then merges the specified
121 message using MergeFrom.
124 other_msg (Message): A message to copy into the current one.
126 if self
is other_msg:
132 """Clears all data that was set in the message."""
133 raise NotImplementedError
136 """Mark this as present in the parent.
138 This normally happens automatically when you assign a field of a
139 sub-message, but sometimes you want to make the sub-message
140 present while keeping it empty. If you find yourself using this,
141 you may want to reconsider your design.
143 raise NotImplementedError
146 """Checks if the message is initialized.
149 bool: The method returns True if the message is initialized (i.e. all of
150 its required fields are set).
152 raise NotImplementedError
161 """Merges serialized protocol buffer data into this message.
163 When we find a field in `serialized` that is already present
166 - If it's a "repeated" field, we append to the end of our list.
167 - Else, if it's a scalar, we overwrite our field.
168 - Else, (it's a nonrepeated composite), we recursively merge
169 into the existing composite.
172 serialized (bytes): Any object that allows us to call
173 ``memoryview(serialized)`` to access a string of bytes using the
177 int: The number of bytes read from `serialized`.
178 For non-group messages, this will always be `len(serialized)`,
179 but for messages which are actually groups, this will
180 generally be less than `len(serialized)`, since we must
181 stop when we reach an ``END_GROUP`` tag. Note that if
182 we *do* stop because of an ``END_GROUP`` tag, the number
183 of bytes returned does not include the bytes
184 for the ``END_GROUP`` tag information.
187 DecodeError: if the input cannot be parsed.
191 raise NotImplementedError
194 """Parse serialized protocol buffer data into this message.
196 Like :func:`MergeFromString()`, except we clear the object first.
202 """Serializes the protocol message to a binary string.
205 deterministic (bool): If true, requests deterministic serialization
206 of the protobuf, with predictable ordering of map keys.
209 A binary string representation of the message if all of the required
210 fields in the message are set (i.e. the message is initialized).
213 EncodeError: if the message isn't initialized (see :func:`IsInitialized`).
215 raise NotImplementedError
218 """Serializes the protocol message to a binary string.
220 This method is similar to SerializeToString but doesn't check if the
221 message is initialized.
224 deterministic (bool): If true, requests deterministic serialization
225 of the protobuf, with predictable ordering of map keys.
228 bytes: A serialized representation of the partial message.
230 raise NotImplementedError
249 """Returns a list of (FieldDescriptor, value) tuples for present fields.
251 A message field is non-empty if HasField() would return true. A singular
252 primitive field is non-empty if HasField() would return true in proto2 or it
253 is non zero in proto3. A repeated field is non-empty if it contains at least
254 one element. The fields are ordered by field number.
257 list[tuple(FieldDescriptor, value)]: field descriptors and values
258 for all fields in the message which are not empty. The values vary by
261 raise NotImplementedError
264 """Checks if a certain field is set for the message.
266 For a oneof group, checks if any field inside is set. Note that if the
267 field_name is not defined in the message descriptor, :exc:`ValueError` will
271 field_name (str): The name of the field to check for presence.
274 bool: Whether a value has been set for the named field.
277 ValueError: if the `field_name` is not a member of this message.
279 raise NotImplementedError
282 """Clears the contents of a given field.
284 Inside a oneof group, clears the field set. If the name neither refers to a
285 defined field or oneof group, :exc:`ValueError` is raised.
288 field_name (str): The name of the field to check for presence.
291 ValueError: if the `field_name` is not a member of this message.
293 raise NotImplementedError
296 """Returns the name of the field that is set inside a oneof group.
298 If no field is set, returns None.
301 oneof_group (str): the name of the oneof group to check.
304 str or None: The name of the group that is set, or None.
307 ValueError: no group with the given name exists
309 raise NotImplementedError
312 """Checks if a certain extension is present for this message.
314 Extensions are retrieved using the :attr:`Extensions` mapping (if present).
317 extension_handle: The handle for the extension to check.
320 bool: Whether the extension is present for this message.
323 KeyError: if the extension is repeated. Similar to repeated fields,
324 there is no separate notion of presence: a "not present" repeated
325 extension is an empty list.
327 raise NotImplementedError
330 """Clears the contents of a given extension.
333 extension_handle: The handle for the extension to clear.
335 raise NotImplementedError
338 """Returns the UnknownFieldSet.
341 UnknownFieldSet: The unknown fields stored in this message.
343 raise NotImplementedError
346 """Clears all fields in the :class:`UnknownFieldSet`.
348 This operation is recursive for nested message.
350 raise NotImplementedError
353 """Returns the serialized size of this message.
355 Recursively calls ByteSize() on all contained messages.
358 int: The number of bytes required to serialize this message.
360 raise NotImplementedError
364 raise NotImplementedError
368 raise NotImplementedError
371 """Internal method used by the protocol message implementation.
372 Clients should not call this directly.
374 Sets a listener that this message will call on certain state transitions.
376 The purpose of this method is to register back-edges from children to
377 parents at runtime, for the purpose of setting "has" bits and
378 byte-size-dirty bits in the parent and ancestor objects whenever a child or
379 descendant object is modified.
381 If the client wants to disconnect this Message from the object tree, she
382 explicitly sets callback to None.
384 If message_listener is None, unregisters any existing listener. Otherwise,
385 message_listener must implement the MessageListener interface in
386 internal/message_listener.py, and we discard any listener registered
387 via a previous _SetListener() call.
389 raise NotImplementedError
392 """Support the pickle protocol."""
396 """Support the pickle protocol."""
398 serialized = state[
'serialized']
401 if not isinstance(serialized, bytes):
402 serialized = serialized.encode(
'latin1')
407 if message_descriptor.containing_type
is None:
412 container = message_descriptor
413 return (_InternalConstructMessage, (container.full_name,),
418 """Constructs a nested message."""
421 return symbol_database.Default().GetSymbol(full_name)()
def HasField(self, field_name)
def __deepcopy__(self, memo=None)
def SerializePartialToString(self, **kwargs)
def __eq__(self, other_msg)
def _InternalConstructMessage(full_name)
def DiscardUnknownFields(self)
def WhichOneof(self, oneof_group)
def ParseFromString(self, serialized)
def _SetListener(self, message_listener)
def SerializeToString(self, **kwargs)
def ClearExtension(self, extension_handle)
def __setstate__(self, state)
def CopyFrom(self, other_msg)
def ClearField(self, field_name)
def HasExtension(self, extension_handle)
def MergeFromString(self, serialized)
def RegisterExtension(extension_handle)
def MergeFrom(self, other_msg)
def __ne__(self, other_msg)
grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:37