message.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. # TODO: We should just make these methods all "pure-virtual" and move
  8. # all implementation out, into reflection.py for now.
  9. """Contains an abstract base class for protocol messages."""
  10. __author__ = 'robinson@google.com (Will Robinson)'
  11. _INCONSISTENT_MESSAGE_ATTRIBUTES = ('Extensions',)
  12. class Error(Exception):
  13. """Base error type for this module."""
  14. pass
  15. class DecodeError(Error):
  16. """Exception raised when deserializing messages."""
  17. pass
  18. class EncodeError(Error):
  19. """Exception raised when serializing messages."""
  20. pass
  21. class FrozenInstanceError(AttributeError):
  22. """Exception raised when mutating a frozen message."""
  23. pass
  24. class Message(object):
  25. """Abstract base class for protocol messages.
  26. Protocol message classes are almost always generated by the protocol
  27. compiler. These generated types subclass Message and implement the methods
  28. shown below.
  29. """
  30. # TODO: Link to an HTML document here.
  31. # TODO: Document that instances of this class will also
  32. # have an Extensions attribute with __getitem__ and __setitem__.
  33. # Again, not sure how to best convey this.
  34. # TODO: Document these fields and methods.
  35. __slots__ = []
  36. #: The :class:`google.protobuf.Descriptor`
  37. # for this message type.
  38. DESCRIPTOR = None
  39. def __deepcopy__(self, memo=None):
  40. clone = type(self)()
  41. clone.MergeFrom(self)
  42. return clone
  43. def __dir__(self):
  44. """Provides the list of all accessible Message attributes."""
  45. message_attributes = set(super().__dir__())
  46. # TODO: Remove this once the UPB implementation is improved.
  47. # The UPB proto implementation currently doesn't provide proto fields as
  48. # attributes and they have to added.
  49. if self.DESCRIPTOR is not None:
  50. for field in self.DESCRIPTOR.fields:
  51. message_attributes.add(field.name)
  52. # The Fast C++ proto implementation provides inaccessible attributes that
  53. # have to be removed.
  54. for attribute in _INCONSISTENT_MESSAGE_ATTRIBUTES:
  55. if attribute not in message_attributes:
  56. continue
  57. try:
  58. getattr(self, attribute)
  59. except AttributeError:
  60. message_attributes.remove(attribute)
  61. return sorted(message_attributes)
  62. def __eq__(self, other_msg):
  63. """Recursively compares two messages by value and structure."""
  64. raise NotImplementedError
  65. def __ne__(self, other_msg):
  66. # Can't just say self != other_msg, since that would infinitely recurse. :)
  67. return not self == other_msg
  68. def __hash__(self):
  69. raise TypeError('unhashable object')
  70. def __str__(self):
  71. """Outputs a human-readable representation of the message."""
  72. raise NotImplementedError
  73. def __unicode__(self):
  74. """Outputs a human-readable representation of the message."""
  75. raise NotImplementedError
  76. def __contains__(self, field_name_or_key):
  77. """Checks if a certain field is set for the message.
  78. Has presence fields return true if the field is set, false if the field is
  79. not set. Fields without presence do raise `ValueError` (this includes
  80. repeated fields, map fields, and implicit presence fields).
  81. If field_name is not defined in the message descriptor, `ValueError` will
  82. be raised.
  83. Note: WKT Struct checks if the key is contained in fields. ListValue checks
  84. if the item is contained in the list.
  85. Args:
  86. field_name_or_key: For Struct, the key (str) of the fields map. For
  87. ListValue, any type that may be contained in the list. For other
  88. messages, name of the field (str) to check for presence.
  89. Returns:
  90. bool: For Struct, whether the item is contained in fields. For ListValue,
  91. whether the item is contained in the list. For other message,
  92. whether a value has been set for the named field.
  93. Raises:
  94. ValueError: For normal messages, if the `field_name_or_key` is not a
  95. member of this message or `field_name_or_key` is not a string.
  96. """
  97. raise NotImplementedError
  98. def MergeFrom(self, other_msg):
  99. """Merges the contents of the specified message into current message.
  100. This method merges the contents of the specified message into the current
  101. message. Singular fields that are set in the specified message overwrite
  102. the corresponding fields in the current message. Repeated fields are
  103. appended. Singular sub-messages and groups are recursively merged.
  104. Args:
  105. other_msg (Message): A message to merge into the current message.
  106. """
  107. raise NotImplementedError
  108. def CopyFrom(self, other_msg):
  109. """Copies the content of the specified message into the current message.
  110. The method clears the current message and then merges the specified
  111. message using MergeFrom.
  112. Args:
  113. other_msg (Message): A message to copy into the current one.
  114. """
  115. if self is other_msg:
  116. return
  117. self.Clear()
  118. self.MergeFrom(other_msg)
  119. def Clear(self):
  120. """Clears all data that was set in the message."""
  121. raise NotImplementedError
  122. def SetInParent(self):
  123. """Mark this as present in the parent.
  124. This normally happens automatically when you assign a field of a
  125. sub-message, but sometimes you want to make the sub-message
  126. present while keeping it empty. If you find yourself using this,
  127. you may want to reconsider your design.
  128. """
  129. raise NotImplementedError
  130. def IsInitialized(self):
  131. """Checks if the message is initialized.
  132. Returns:
  133. bool: The method returns True if the message is initialized (i.e. all of
  134. its required fields are set).
  135. """
  136. raise NotImplementedError
  137. # TODO: MergeFromString() should probably return None and be
  138. # implemented in terms of a helper that returns the # of bytes read. Our
  139. # deserialization routines would use the helper when recursively
  140. # deserializing, but the end user would almost always just want the no-return
  141. # MergeFromString().
  142. def MergeFromString(self, serialized):
  143. """Merges serialized protocol buffer data into this message.
  144. When we find a field in `serialized` that is already present
  145. in this message:
  146. - If it's a "repeated" field, we append to the end of our list.
  147. - Else, if it's a scalar, we overwrite our field.
  148. - Else, (it's a nonrepeated composite), we recursively merge
  149. into the existing composite.
  150. Args:
  151. serialized (bytes): Any object that allows us to call
  152. ``memoryview(serialized)`` to access a string of bytes using the buffer
  153. interface.
  154. Returns:
  155. int: The number of bytes read from `serialized`.
  156. For non-group messages, this will always be `len(serialized)`,
  157. but for messages which are actually groups, this will
  158. generally be less than `len(serialized)`, since we must
  159. stop when we reach an ``END_GROUP`` tag. Note that if
  160. we *do* stop because of an ``END_GROUP`` tag, the number
  161. of bytes returned does not include the bytes
  162. for the ``END_GROUP`` tag information.
  163. Raises:
  164. DecodeError: if the input cannot be parsed.
  165. """
  166. # TODO: Document handling of unknown fields.
  167. # TODO: When we switch to a helper, this will return None.
  168. raise NotImplementedError
  169. def ParseFromString(self, serialized):
  170. """Parse serialized protocol buffer data in binary form into this message.
  171. Like :func:`MergeFromString()`, except we clear the object first.
  172. Raises:
  173. message.DecodeError if the input cannot be parsed.
  174. """
  175. self.Clear()
  176. return self.MergeFromString(serialized)
  177. def SerializeToString(self, **kwargs):
  178. """Serializes the protocol message to a binary string.
  179. Keyword Args:
  180. deterministic (bool): If true, requests deterministic serialization
  181. of the protobuf. Note that there is no canonical representation of
  182. protobuf messages: deterministic serialization only means 'consistent
  183. for current build, but not stable between rebuilds, and may not match
  184. decisions made by other languages'.
  185. See
  186. https://protobuf.dev/programming-guides/serialization-not-canonical/.
  187. Returns:
  188. A binary string representation of the message if all of the required
  189. fields in the message are set (i.e. the message is initialized).
  190. Raises:
  191. EncodeError: if the message isn't initialized (see :func:`IsInitialized`).
  192. """
  193. raise NotImplementedError
  194. def SerializePartialToString(self, **kwargs):
  195. """Serializes the protocol message to a binary string.
  196. This method is similar to SerializeToString but doesn't check if the
  197. message is initialized.
  198. Keyword Args:
  199. deterministic (bool): If true, requests deterministic serialization
  200. of the protobuf. Note that 'deterministic' serialization only means
  201. 'Consistent for current build, but still arbitary and not stable over
  202. time'. There is no canonical representation of protobuf messages,
  203. See
  204. https://protobuf.dev/programming-guides/serialization-not-canonical/.
  205. Returns:
  206. bytes: A serialized representation of the partial message.
  207. """
  208. raise NotImplementedError
  209. # TODO: Decide whether we like these better
  210. # than auto-generated has_foo() and clear_foo() methods
  211. # on the instances themselves. This way is less consistent
  212. # with C++, but it makes reflection-type access easier and
  213. # reduces the number of magically autogenerated things.
  214. #
  215. # TODO: Be sure to document (and test) exactly
  216. # which field names are accepted here. Are we case-sensitive?
  217. # What do we do with fields that share names with Python keywords
  218. # like 'lambda' and 'yield'?
  219. #
  220. # nnorwitz says:
  221. # """
  222. # Typically (in python), an underscore is appended to names that are
  223. # keywords. So they would become lambda_ or yield_.
  224. # """
  225. def ListFields(self):
  226. """Returns a list of (FieldDescriptor, value) tuples for present fields.
  227. A message field is non-empty if HasField() would return true. A singular
  228. primitive field is non-empty if HasField() would return true in proto2 or it
  229. is non zero in proto3. A repeated field is non-empty if it contains at least
  230. one element. The fields are ordered by field number.
  231. Returns:
  232. list[tuple(FieldDescriptor, value)]: field descriptors and values
  233. for all fields in the message which are not empty. The values vary by
  234. field type.
  235. """
  236. raise NotImplementedError
  237. def HasField(self, field_name):
  238. """Checks if a certain field is set for the message.
  239. For a oneof group, checks if any field inside is set. Note that if the
  240. field_name is not defined in the message descriptor, :exc:`ValueError` will
  241. be raised.
  242. Args:
  243. field_name (str): The name of the field to check for presence.
  244. Returns:
  245. bool: Whether a value has been set for the named field.
  246. Raises:
  247. ValueError: if the `field_name` is not a member of this message.
  248. """
  249. raise NotImplementedError
  250. def ClearField(self, field_name):
  251. """Clears the contents of a given field.
  252. Inside a oneof group, clears the field set. If the name neither refers to a
  253. defined field or oneof group, :exc:`ValueError` is raised.
  254. Args:
  255. field_name (str): The name of the field to check for presence.
  256. Raises:
  257. ValueError: if the `field_name` is not a member of this message.
  258. """
  259. raise NotImplementedError
  260. def WhichOneof(self, oneof_group):
  261. """Returns the name of the field that is set inside a oneof group.
  262. If no field is set, returns None.
  263. Args:
  264. oneof_group (str): the name of the oneof group to check.
  265. Returns:
  266. str or None: The name of the group that is set, or None.
  267. Raises:
  268. ValueError: no group with the given name exists
  269. """
  270. raise NotImplementedError
  271. def HasExtension(self, field_descriptor):
  272. """Checks if a certain extension is present for this message.
  273. Extensions are retrieved using the :attr:`Extensions` mapping (if present).
  274. Args:
  275. field_descriptor: The field descriptor for the extension to check.
  276. Returns:
  277. bool: Whether the extension is present for this message.
  278. Raises:
  279. KeyError: if the extension is repeated. Similar to repeated fields,
  280. there is no separate notion of presence: a "not present" repeated
  281. extension is an empty list.
  282. """
  283. raise NotImplementedError
  284. def ClearExtension(self, field_descriptor):
  285. """Clears the contents of a given extension.
  286. Args:
  287. field_descriptor: The field descriptor for the extension to clear.
  288. """
  289. raise NotImplementedError
  290. def UnknownFields(self):
  291. """Returns the UnknownFieldSet.
  292. Returns:
  293. UnknownFieldSet: The unknown fields stored in this message.
  294. """
  295. raise NotImplementedError
  296. def DiscardUnknownFields(self):
  297. """Clears all fields in the :class:`UnknownFieldSet`.
  298. This operation is recursive for nested message.
  299. """
  300. raise NotImplementedError
  301. def ByteSize(self):
  302. """Returns the serialized size of this message.
  303. Recursively calls ByteSize() on all contained messages.
  304. Returns:
  305. int: The number of bytes required to serialize this message.
  306. """
  307. raise NotImplementedError
  308. @classmethod
  309. def FromString(cls, s):
  310. raise NotImplementedError
  311. def _SetListener(self, message_listener):
  312. """Internal method used by the protocol message implementation.
  313. Clients should not call this directly.
  314. Sets a listener that this message will call on certain state transitions.
  315. The purpose of this method is to register back-edges from children to
  316. parents at runtime, for the purpose of setting "has" bits and
  317. byte-size-dirty bits in the parent and ancestor objects whenever a child or
  318. descendant object is modified.
  319. If the client wants to disconnect this Message from the object tree, she
  320. explicitly sets callback to None.
  321. If message_listener is None, unregisters any existing listener. Otherwise,
  322. message_listener must implement the MessageListener interface in
  323. internal/message_listener.py, and we discard any listener registered
  324. via a previous _SetListener() call.
  325. """
  326. raise NotImplementedError
  327. def __getstate__(self):
  328. """Support the pickle protocol."""
  329. return dict(serialized=self.SerializePartialToString())
  330. def __setstate__(self, state):
  331. """Support the pickle protocol."""
  332. self.__init__()
  333. serialized = state['serialized']
  334. # On Python 3, using encoding='latin1' is required for unpickling
  335. # protos pickled by Python 2.
  336. if not isinstance(serialized, bytes):
  337. serialized = serialized.encode('latin1')
  338. self.ParseFromString(serialized)
  339. def __reduce__(self):
  340. message_descriptor = self.DESCRIPTOR
  341. if message_descriptor.containing_type is None:
  342. return type(self), (), self.__getstate__()
  343. # the message type must be nested.
  344. # Python does not pickle nested classes; use the symbol_database on the
  345. # receiving end.
  346. container = message_descriptor
  347. return (
  348. _InternalConstructMessage,
  349. (container.full_name,),
  350. self.__getstate__(),
  351. )
  352. def _InternalConstructMessage(full_name):
  353. """Constructs a nested message."""
  354. from google.protobuf import symbol_database # pylint:disable=g-import-not-at-top
  355. return symbol_database.Default().GetSymbol(full_name)()