message.py 15 KB

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