python_message.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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. # This code is meant to work on Python 2.4 and above only.
  8. #
  9. # TODO: Helpers for verbose, common checks like seeing if a
  10. # descriptor's cpp_type is CPPTYPE_MESSAGE.
  11. """Contains a metaclass and helper functions used to create
  12. protocol message classes from Descriptor objects at runtime.
  13. Recall that a metaclass is the "type" of a class.
  14. (A class is to a metaclass what an instance is to a class.)
  15. In this case, we use the GeneratedProtocolMessageType metaclass
  16. to inject all the useful functionality into the classes
  17. output by the protocol compiler at compile-time.
  18. The upshot of all this is that the real implementation
  19. details for ALL pure-Python protocol buffers are *here in
  20. this file*.
  21. """
  22. __author__ = 'robinson@google.com (Will Robinson)'
  23. import datetime
  24. from io import BytesIO
  25. import struct
  26. import sys
  27. import warnings
  28. import weakref
  29. from google.protobuf import descriptor as descriptor_mod
  30. from google.protobuf import message as message_mod
  31. from google.protobuf import text_format
  32. # We use "as" to avoid name collisions with variables.
  33. from google.protobuf.internal import api_implementation
  34. from google.protobuf.internal import containers
  35. from google.protobuf.internal import decoder
  36. from google.protobuf.internal import encoder
  37. from google.protobuf.internal import enum_type_wrapper
  38. from google.protobuf.internal import extension_dict
  39. from google.protobuf.internal import message_listener as message_listener_mod
  40. from google.protobuf.internal import type_checkers
  41. from google.protobuf.internal import well_known_types
  42. from google.protobuf.internal import wire_format
  43. _FieldDescriptor = descriptor_mod.FieldDescriptor
  44. _AnyFullTypeName = 'google.protobuf.Any'
  45. _StructFullTypeName = 'google.protobuf.Struct'
  46. _ListValueFullTypeName = 'google.protobuf.ListValue'
  47. _ExtensionDict = extension_dict._ExtensionDict
  48. class GeneratedProtocolMessageType(type):
  49. """Metaclass for protocol message classes created at runtime from Descriptors.
  50. We add implementations for all methods described in the Message class. We
  51. also create properties to allow getting/setting all fields in the protocol
  52. message. Finally, we create slots to prevent users from accidentally
  53. "setting" nonexistent fields in the protocol message, which then wouldn't get
  54. serialized / deserialized properly.
  55. The protocol compiler currently uses this metaclass to create protocol
  56. message classes at runtime. Clients can also manually create their own
  57. classes at runtime, as in this example:
  58. mydescriptor = Descriptor(.....)
  59. factory = symbol_database.Default()
  60. factory.pool.AddDescriptor(mydescriptor)
  61. MyProtoClass = message_factory.GetMessageClass(mydescriptor)
  62. myproto_instance = MyProtoClass()
  63. myproto.foo_field = 23
  64. ...
  65. """
  66. # Must be consistent with the protocol-compiler code in
  67. # proto2/compiler/internal/generator.*.
  68. _DESCRIPTOR_KEY = 'DESCRIPTOR'
  69. def __new__(cls, name, bases, dictionary):
  70. """Custom allocation for runtime-generated class types.
  71. We override __new__ because this is apparently the only place
  72. where we can meaningfully set __slots__ on the class we're creating(?).
  73. (The interplay between metaclasses and slots is not very well-documented).
  74. Args:
  75. name: Name of the class (ignored, but required by the
  76. metaclass protocol).
  77. bases: Base classes of the class we're constructing.
  78. (Should be message.Message). We ignore this field, but
  79. it's required by the metaclass protocol
  80. dictionary: The class dictionary of the class we're
  81. constructing. dictionary[_DESCRIPTOR_KEY] must contain
  82. a Descriptor object describing this protocol message
  83. type.
  84. Returns:
  85. Newly-allocated class.
  86. Raises:
  87. RuntimeError: Generated code only work with python cpp extension.
  88. """
  89. descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY]
  90. if isinstance(descriptor, str):
  91. raise RuntimeError('The generated code only work with python cpp '
  92. 'extension, but it is using pure python runtime.')
  93. # If a concrete class already exists for this descriptor, don't try to
  94. # create another. Doing so will break any messages that already exist with
  95. # the existing class.
  96. #
  97. # The C++ implementation appears to have its own internal `PyMessageFactory`
  98. # to achieve similar results.
  99. #
  100. # This most commonly happens in `text_format.py` when using descriptors from
  101. # a custom pool; it calls message_factory.GetMessageClass() on a
  102. # descriptor which already has an existing concrete class.
  103. new_class = getattr(descriptor, '_concrete_class', None)
  104. if new_class:
  105. return new_class
  106. if descriptor.full_name in well_known_types.WKTBASES:
  107. bases += (well_known_types.WKTBASES[descriptor.full_name],)
  108. _AddClassAttributesForNestedExtensions(descriptor, dictionary)
  109. _AddSlots(descriptor, dictionary)
  110. superclass = super(GeneratedProtocolMessageType, cls)
  111. new_class = superclass.__new__(cls, name, bases, dictionary)
  112. return new_class
  113. def __init__(cls, name, bases, dictionary):
  114. """Here we perform the majority of our work on the class.
  115. We add enum getters, an __init__ method, implementations
  116. of all Message methods, and properties for all fields
  117. in the protocol type.
  118. Args:
  119. name: Name of the class (ignored, but required by the
  120. metaclass protocol).
  121. bases: Base classes of the class we're constructing.
  122. (Should be message.Message). We ignore this field, but
  123. it's required by the metaclass protocol
  124. dictionary: The class dictionary of the class we're
  125. constructing. dictionary[_DESCRIPTOR_KEY] must contain
  126. a Descriptor object describing this protocol message
  127. type.
  128. """
  129. descriptor = dictionary[GeneratedProtocolMessageType._DESCRIPTOR_KEY]
  130. # If this is an _existing_ class looked up via `_concrete_class` in the
  131. # __new__ method above, then we don't need to re-initialize anything.
  132. existing_class = getattr(descriptor, '_concrete_class', None)
  133. if existing_class:
  134. assert existing_class is cls, (
  135. 'Duplicate `GeneratedProtocolMessageType` created for descriptor %r'
  136. % (descriptor.full_name))
  137. return
  138. cls._message_set_decoders_by_tag = {}
  139. cls._fields_by_tag = {}
  140. if (descriptor.has_options and
  141. descriptor.GetOptions().message_set_wire_format):
  142. cls._message_set_decoders_by_tag[decoder.MESSAGE_SET_ITEM_TAG] = (
  143. decoder.MessageSetItemDecoder(descriptor),
  144. None,
  145. )
  146. # Attach stuff to each FieldDescriptor for quick lookup later on.
  147. for field in descriptor.fields:
  148. _AttachFieldHelpers(cls, field)
  149. if descriptor.is_extendable and hasattr(descriptor.file, 'pool'):
  150. extensions = descriptor.file.pool.FindAllExtensions(descriptor)
  151. for ext in extensions:
  152. _AttachFieldHelpers(cls, ext)
  153. descriptor._concrete_class = cls # pylint: disable=protected-access
  154. _AddEnumValues(descriptor, cls)
  155. _AddInitMethod(descriptor, cls)
  156. _AddPropertiesForFields(descriptor, cls)
  157. _AddPropertiesForExtensions(descriptor, cls)
  158. _AddStaticMethods(cls)
  159. _AddMessageMethods(descriptor, cls)
  160. _AddPrivateHelperMethods(descriptor, cls)
  161. superclass = super(GeneratedProtocolMessageType, cls)
  162. superclass.__init__(name, bases, dictionary)
  163. # Stateless helpers for GeneratedProtocolMessageType below.
  164. # Outside clients should not access these directly.
  165. #
  166. # I opted not to make any of these methods on the metaclass, to make it more
  167. # clear that I'm not really using any state there and to keep clients from
  168. # thinking that they have direct access to these construction helpers.
  169. def _PropertyName(proto_field_name):
  170. """Returns the name of the public property attribute which
  171. clients can use to get and (in some cases) set the value
  172. of a protocol message field.
  173. Args:
  174. proto_field_name: The protocol message field name, exactly
  175. as it appears (or would appear) in a .proto file.
  176. """
  177. # TODO: Escape Python keywords (e.g., yield), and test this support.
  178. # nnorwitz makes my day by writing:
  179. # """
  180. # FYI. See the keyword module in the stdlib. This could be as simple as:
  181. #
  182. # if keyword.iskeyword(proto_field_name):
  183. # return proto_field_name + "_"
  184. # return proto_field_name
  185. # """
  186. # Kenton says: The above is a BAD IDEA. People rely on being able to use
  187. # getattr() and setattr() to reflectively manipulate field values. If we
  188. # rename the properties, then every such user has to also make sure to apply
  189. # the same transformation. Note that currently if you name a field "yield",
  190. # you can still access it just fine using getattr/setattr -- it's not even
  191. # that cumbersome to do so.
  192. # TODO: Remove this method entirely if/when everyone agrees with my
  193. # position.
  194. return proto_field_name
  195. def _AddSlots(message_descriptor, dictionary):
  196. """Adds a __slots__ entry to dictionary, containing the names of all valid
  197. attributes for this message type.
  198. Args:
  199. message_descriptor: A Descriptor instance describing this message type.
  200. dictionary: Class dictionary to which we'll add a '__slots__' entry.
  201. """
  202. dictionary['__slots__'] = ['_cached_byte_size',
  203. '_cached_byte_size_dirty',
  204. '_fields',
  205. '_unknown_fields',
  206. '_is_present_in_parent',
  207. '_listener',
  208. '_listener_for_children',
  209. '__weakref__',
  210. '_oneofs']
  211. def _IsMessageSetExtension(field):
  212. return (field.is_extension and
  213. field.containing_type.has_options and
  214. field.containing_type.GetOptions().message_set_wire_format and
  215. field.type == _FieldDescriptor.TYPE_MESSAGE and
  216. field.label == _FieldDescriptor.LABEL_OPTIONAL)
  217. def _IsMapField(field):
  218. return (field.type == _FieldDescriptor.TYPE_MESSAGE and
  219. field.message_type._is_map_entry)
  220. def _IsMessageMapField(field):
  221. value_type = field.message_type.fields_by_name['value']
  222. return value_type.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE
  223. def _AttachFieldHelpers(cls, field_descriptor):
  224. is_repeated = field_descriptor.label == _FieldDescriptor.LABEL_REPEATED
  225. field_descriptor._default_constructor = _DefaultValueConstructorForField(
  226. field_descriptor
  227. )
  228. def AddFieldByTag(wiretype, is_packed):
  229. tag_bytes = encoder.TagBytes(field_descriptor.number, wiretype)
  230. cls._fields_by_tag[tag_bytes] = (field_descriptor, is_packed)
  231. AddFieldByTag(
  232. type_checkers.FIELD_TYPE_TO_WIRE_TYPE[field_descriptor.type], False
  233. )
  234. if is_repeated and wire_format.IsTypePackable(field_descriptor.type):
  235. # To support wire compatibility of adding packed = true, add a decoder for
  236. # packed values regardless of the field's options.
  237. AddFieldByTag(wire_format.WIRETYPE_LENGTH_DELIMITED, True)
  238. def _MaybeAddEncoder(cls, field_descriptor):
  239. if hasattr(field_descriptor, '_encoder'):
  240. return
  241. is_repeated = (field_descriptor.label == _FieldDescriptor.LABEL_REPEATED)
  242. is_map_entry = _IsMapField(field_descriptor)
  243. is_packed = field_descriptor.is_packed
  244. if is_map_entry:
  245. field_encoder = encoder.MapEncoder(field_descriptor)
  246. sizer = encoder.MapSizer(field_descriptor,
  247. _IsMessageMapField(field_descriptor))
  248. elif _IsMessageSetExtension(field_descriptor):
  249. field_encoder = encoder.MessageSetItemEncoder(field_descriptor.number)
  250. sizer = encoder.MessageSetItemSizer(field_descriptor.number)
  251. else:
  252. field_encoder = type_checkers.TYPE_TO_ENCODER[field_descriptor.type](
  253. field_descriptor.number, is_repeated, is_packed)
  254. sizer = type_checkers.TYPE_TO_SIZER[field_descriptor.type](
  255. field_descriptor.number, is_repeated, is_packed)
  256. field_descriptor._sizer = sizer
  257. field_descriptor._encoder = field_encoder
  258. def _MaybeAddDecoder(cls, field_descriptor):
  259. if hasattr(field_descriptor, '_decoders'):
  260. return
  261. is_repeated = field_descriptor.label == _FieldDescriptor.LABEL_REPEATED
  262. is_map_entry = _IsMapField(field_descriptor)
  263. helper_decoders = {}
  264. def AddDecoder(is_packed):
  265. decode_type = field_descriptor.type
  266. if (decode_type == _FieldDescriptor.TYPE_ENUM and
  267. not field_descriptor.enum_type.is_closed):
  268. decode_type = _FieldDescriptor.TYPE_INT32
  269. oneof_descriptor = None
  270. if field_descriptor.containing_oneof is not None:
  271. oneof_descriptor = field_descriptor
  272. if is_map_entry:
  273. is_message_map = _IsMessageMapField(field_descriptor)
  274. field_decoder = decoder.MapDecoder(
  275. field_descriptor, _GetInitializeDefaultForMap(field_descriptor),
  276. is_message_map)
  277. elif decode_type == _FieldDescriptor.TYPE_STRING:
  278. field_decoder = decoder.StringDecoder(
  279. field_descriptor.number, is_repeated, is_packed,
  280. field_descriptor, field_descriptor._default_constructor,
  281. not field_descriptor.has_presence)
  282. elif field_descriptor.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  283. field_decoder = type_checkers.TYPE_TO_DECODER[decode_type](
  284. field_descriptor.number, is_repeated, is_packed,
  285. field_descriptor, field_descriptor._default_constructor)
  286. else:
  287. field_decoder = type_checkers.TYPE_TO_DECODER[decode_type](
  288. field_descriptor.number, is_repeated, is_packed,
  289. # pylint: disable=protected-access
  290. field_descriptor, field_descriptor._default_constructor,
  291. not field_descriptor.has_presence)
  292. helper_decoders[is_packed] = field_decoder
  293. AddDecoder(False)
  294. if is_repeated and wire_format.IsTypePackable(field_descriptor.type):
  295. # To support wire compatibility of adding packed = true, add a decoder for
  296. # packed values regardless of the field's options.
  297. AddDecoder(True)
  298. field_descriptor._decoders = helper_decoders
  299. def _AddClassAttributesForNestedExtensions(descriptor, dictionary):
  300. extensions = descriptor.extensions_by_name
  301. for extension_name, extension_field in extensions.items():
  302. assert extension_name not in dictionary
  303. dictionary[extension_name] = extension_field
  304. def _AddEnumValues(descriptor, cls):
  305. """Sets class-level attributes for all enum fields defined in this message.
  306. Also exporting a class-level object that can name enum values.
  307. Args:
  308. descriptor: Descriptor object for this message type.
  309. cls: Class we're constructing for this message type.
  310. """
  311. for enum_type in descriptor.enum_types:
  312. setattr(cls, enum_type.name, enum_type_wrapper.EnumTypeWrapper(enum_type))
  313. for enum_value in enum_type.values:
  314. setattr(cls, enum_value.name, enum_value.number)
  315. def _GetInitializeDefaultForMap(field):
  316. if field.label != _FieldDescriptor.LABEL_REPEATED:
  317. raise ValueError('map_entry set on non-repeated field %s' % (
  318. field.name))
  319. fields_by_name = field.message_type.fields_by_name
  320. key_checker = type_checkers.GetTypeChecker(fields_by_name['key'])
  321. value_field = fields_by_name['value']
  322. if _IsMessageMapField(field):
  323. def MakeMessageMapDefault(message):
  324. return containers.MessageMap(
  325. message._listener_for_children, value_field.message_type, key_checker,
  326. field.message_type)
  327. return MakeMessageMapDefault
  328. else:
  329. value_checker = type_checkers.GetTypeChecker(value_field)
  330. def MakePrimitiveMapDefault(message):
  331. return containers.ScalarMap(
  332. message._listener_for_children, key_checker, value_checker,
  333. field.message_type)
  334. return MakePrimitiveMapDefault
  335. def _DefaultValueConstructorForField(field):
  336. """Returns a function which returns a default value for a field.
  337. Args:
  338. field: FieldDescriptor object for this field.
  339. The returned function has one argument:
  340. message: Message instance containing this field, or a weakref proxy
  341. of same.
  342. That function in turn returns a default value for this field. The default
  343. value may refer back to |message| via a weak reference.
  344. """
  345. if _IsMapField(field):
  346. return _GetInitializeDefaultForMap(field)
  347. if field.label == _FieldDescriptor.LABEL_REPEATED:
  348. if field.has_default_value and field.default_value != []:
  349. raise ValueError('Repeated field default value not empty list: %s' % (
  350. field.default_value))
  351. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  352. # We can't look at _concrete_class yet since it might not have
  353. # been set. (Depends on order in which we initialize the classes).
  354. message_type = field.message_type
  355. def MakeRepeatedMessageDefault(message):
  356. return containers.RepeatedCompositeFieldContainer(
  357. message._listener_for_children, field.message_type)
  358. return MakeRepeatedMessageDefault
  359. else:
  360. type_checker = type_checkers.GetTypeChecker(field)
  361. def MakeRepeatedScalarDefault(message):
  362. return containers.RepeatedScalarFieldContainer(
  363. message._listener_for_children, type_checker)
  364. return MakeRepeatedScalarDefault
  365. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  366. message_type = field.message_type
  367. def MakeSubMessageDefault(message):
  368. # _concrete_class may not yet be initialized.
  369. if not hasattr(message_type, '_concrete_class'):
  370. from google.protobuf import message_factory
  371. message_factory.GetMessageClass(message_type)
  372. result = message_type._concrete_class()
  373. result._SetListener(
  374. _OneofListener(message, field)
  375. if field.containing_oneof is not None
  376. else message._listener_for_children)
  377. return result
  378. return MakeSubMessageDefault
  379. def MakeScalarDefault(message):
  380. # TODO: This may be broken since there may not be
  381. # default_value. Combine with has_default_value somehow.
  382. return field.default_value
  383. return MakeScalarDefault
  384. def _ReraiseTypeErrorWithFieldName(message_name, field_name):
  385. """Re-raise the currently-handled TypeError with the field name added."""
  386. exc = sys.exc_info()[1]
  387. if len(exc.args) == 1 and type(exc) is TypeError:
  388. # simple TypeError; add field name to exception message
  389. exc = TypeError('%s for field %s.%s' % (str(exc), message_name, field_name))
  390. # re-raise possibly-amended exception with original traceback:
  391. raise exc.with_traceback(sys.exc_info()[2])
  392. def _AddInitMethod(message_descriptor, cls):
  393. """Adds an __init__ method to cls."""
  394. def _GetIntegerEnumValue(enum_type, value):
  395. """Convert a string or integer enum value to an integer.
  396. If the value is a string, it is converted to the enum value in
  397. enum_type with the same name. If the value is not a string, it's
  398. returned as-is. (No conversion or bounds-checking is done.)
  399. """
  400. if isinstance(value, str):
  401. try:
  402. return enum_type.values_by_name[value].number
  403. except KeyError:
  404. raise ValueError('Enum type %s: unknown label "%s"' % (
  405. enum_type.full_name, value))
  406. return value
  407. def init(self, **kwargs):
  408. self._cached_byte_size = 0
  409. self._cached_byte_size_dirty = len(kwargs) > 0
  410. self._fields = {}
  411. # Contains a mapping from oneof field descriptors to the descriptor
  412. # of the currently set field in that oneof field.
  413. self._oneofs = {}
  414. # _unknown_fields is () when empty for efficiency, and will be turned into
  415. # a list if fields are added.
  416. self._unknown_fields = ()
  417. self._is_present_in_parent = False
  418. self._listener = message_listener_mod.NullMessageListener()
  419. self._listener_for_children = _Listener(self)
  420. for field_name, field_value in kwargs.items():
  421. field = _GetFieldByName(message_descriptor, field_name)
  422. if field is None:
  423. raise TypeError('%s() got an unexpected keyword argument "%s"' %
  424. (message_descriptor.name, field_name))
  425. if field_value is None:
  426. # field=None is the same as no field at all.
  427. continue
  428. if field.label == _FieldDescriptor.LABEL_REPEATED:
  429. field_copy = field._default_constructor(self)
  430. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE: # Composite
  431. if _IsMapField(field):
  432. if _IsMessageMapField(field):
  433. for key in field_value:
  434. field_copy[key].MergeFrom(field_value[key])
  435. else:
  436. field_copy.update(field_value)
  437. else:
  438. for val in field_value:
  439. if isinstance(val, dict):
  440. field_copy.add(**val)
  441. else:
  442. field_copy.add().MergeFrom(val)
  443. else: # Scalar
  444. if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
  445. field_value = [_GetIntegerEnumValue(field.enum_type, val)
  446. for val in field_value]
  447. field_copy.extend(field_value)
  448. self._fields[field] = field_copy
  449. elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  450. field_copy = field._default_constructor(self)
  451. new_val = None
  452. if isinstance(field_value, message_mod.Message):
  453. new_val = field_value
  454. elif isinstance(field_value, dict):
  455. if field.message_type.full_name == _StructFullTypeName:
  456. field_copy.Clear()
  457. if len(field_value) == 1 and 'fields' in field_value:
  458. try:
  459. field_copy.update(field_value)
  460. except:
  461. # Fall back to init normal message field
  462. field_copy.Clear()
  463. new_val = field.message_type._concrete_class(**field_value)
  464. else:
  465. field_copy.update(field_value)
  466. else:
  467. new_val = field.message_type._concrete_class(**field_value)
  468. elif hasattr(field_copy, '_internal_assign'):
  469. field_copy._internal_assign(field_value)
  470. else:
  471. raise TypeError(
  472. 'Message field {0}.{1} must be initialized with a '
  473. 'dict or instance of same class, got {2}.'.format(
  474. message_descriptor.name,
  475. field_name,
  476. type(field_value).__name__,
  477. )
  478. )
  479. if new_val != None:
  480. try:
  481. field_copy.MergeFrom(new_val)
  482. except TypeError:
  483. _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
  484. self._fields[field] = field_copy
  485. else:
  486. if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
  487. field_value = _GetIntegerEnumValue(field.enum_type, field_value)
  488. try:
  489. setattr(self, field_name, field_value)
  490. except TypeError:
  491. _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
  492. init.__module__ = None
  493. init.__doc__ = None
  494. cls.__init__ = init
  495. def _GetFieldByName(message_descriptor, field_name):
  496. """Returns a field descriptor by field name.
  497. Args:
  498. message_descriptor: A Descriptor describing all fields in message.
  499. field_name: The name of the field to retrieve.
  500. Returns:
  501. The field descriptor associated with the field name.
  502. """
  503. try:
  504. return message_descriptor.fields_by_name[field_name]
  505. except KeyError:
  506. raise ValueError('Protocol message %s has no "%s" field.' %
  507. (message_descriptor.name, field_name))
  508. def _AddPropertiesForFields(descriptor, cls):
  509. """Adds properties for all fields in this protocol message type."""
  510. for field in descriptor.fields:
  511. _AddPropertiesForField(field, cls)
  512. if descriptor.is_extendable:
  513. # _ExtensionDict is just an adaptor with no state so we allocate a new one
  514. # every time it is accessed.
  515. cls.Extensions = property(lambda self: _ExtensionDict(self))
  516. def _AddPropertiesForField(field, cls):
  517. """Adds a public property for a protocol message field.
  518. Clients can use this property to get and (in the case
  519. of non-repeated scalar fields) directly set the value
  520. of a protocol message field.
  521. Args:
  522. field: A FieldDescriptor for this field.
  523. cls: The class we're constructing.
  524. """
  525. # Catch it if we add other types that we should
  526. # handle specially here.
  527. assert _FieldDescriptor.MAX_CPPTYPE == 10
  528. constant_name = field.name.upper() + '_FIELD_NUMBER'
  529. setattr(cls, constant_name, field.number)
  530. if field.label == _FieldDescriptor.LABEL_REPEATED:
  531. _AddPropertiesForRepeatedField(field, cls)
  532. elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  533. _AddPropertiesForNonRepeatedCompositeField(field, cls)
  534. else:
  535. _AddPropertiesForNonRepeatedScalarField(field, cls)
  536. class _FieldProperty(property):
  537. __slots__ = ('DESCRIPTOR',)
  538. def __init__(self, descriptor, getter, setter, doc):
  539. property.__init__(self, getter, setter, doc=doc)
  540. self.DESCRIPTOR = descriptor
  541. def _AddPropertiesForRepeatedField(field, cls):
  542. """Adds a public property for a "repeated" protocol message field. Clients
  543. can use this property to get the value of the field, which will be either a
  544. RepeatedScalarFieldContainer or RepeatedCompositeFieldContainer (see
  545. below).
  546. Note that when clients add values to these containers, we perform
  547. type-checking in the case of repeated scalar fields, and we also set any
  548. necessary "has" bits as a side-effect.
  549. Args:
  550. field: A FieldDescriptor for this field.
  551. cls: The class we're constructing.
  552. """
  553. proto_field_name = field.name
  554. property_name = _PropertyName(proto_field_name)
  555. def getter(self):
  556. field_value = self._fields.get(field)
  557. if field_value is None:
  558. # Construct a new object to represent this field.
  559. field_value = field._default_constructor(self)
  560. # Atomically check if another thread has preempted us and, if not, swap
  561. # in the new object we just created. If someone has preempted us, we
  562. # take that object and discard ours.
  563. # WARNING: We are relying on setdefault() being atomic. This is true
  564. # in CPython but we haven't investigated others. This warning appears
  565. # in several other locations in this file.
  566. field_value = self._fields.setdefault(field, field_value)
  567. return field_value
  568. getter.__module__ = None
  569. getter.__doc__ = 'Getter for %s.' % proto_field_name
  570. # We define a setter just so we can throw an exception with a more
  571. # helpful error message.
  572. def setter(self, new_value):
  573. raise AttributeError('Assignment not allowed to repeated field '
  574. '"%s" in protocol message object.' % proto_field_name)
  575. doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
  576. setattr(cls, property_name, _FieldProperty(field, getter, setter, doc=doc))
  577. def _AddPropertiesForNonRepeatedScalarField(field, cls):
  578. """Adds a public property for a nonrepeated, scalar protocol message field.
  579. Clients can use this property to get and directly set the value of the field.
  580. Note that when the client sets the value of a field by using this property,
  581. all necessary "has" bits are set as a side-effect, and we also perform
  582. type-checking.
  583. Args:
  584. field: A FieldDescriptor for this field.
  585. cls: The class we're constructing.
  586. """
  587. proto_field_name = field.name
  588. property_name = _PropertyName(proto_field_name)
  589. type_checker = type_checkers.GetTypeChecker(field)
  590. default_value = field.default_value
  591. def getter(self):
  592. # TODO: This may be broken since there may not be
  593. # default_value. Combine with has_default_value somehow.
  594. return self._fields.get(field, default_value)
  595. getter.__module__ = None
  596. getter.__doc__ = 'Getter for %s.' % proto_field_name
  597. def field_setter(self, new_value):
  598. # pylint: disable=protected-access
  599. # Testing the value for truthiness captures all of the proto3 defaults
  600. # (0, 0.0, enum 0, and False).
  601. try:
  602. new_value = type_checker.CheckValue(new_value)
  603. except TypeError as e:
  604. raise TypeError(
  605. 'Cannot set %s to %.1024r: %s' % (field.full_name, new_value, e))
  606. if not field.has_presence and not new_value:
  607. self._fields.pop(field, None)
  608. else:
  609. self._fields[field] = new_value
  610. # Check _cached_byte_size_dirty inline to improve performance, since scalar
  611. # setters are called frequently.
  612. if not self._cached_byte_size_dirty:
  613. self._Modified()
  614. if field.containing_oneof:
  615. def setter(self, new_value):
  616. field_setter(self, new_value)
  617. self._UpdateOneofState(field)
  618. else:
  619. setter = field_setter
  620. setter.__module__ = None
  621. setter.__doc__ = 'Setter for %s.' % proto_field_name
  622. # Add a property to encapsulate the getter/setter.
  623. doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
  624. setattr(cls, property_name, _FieldProperty(field, getter, setter, doc=doc))
  625. def _AddPropertiesForNonRepeatedCompositeField(field, cls):
  626. """Adds a public property for a nonrepeated, composite protocol message field.
  627. A composite field is a "group" or "message" field.
  628. Clients can use this property to get the value of the field, but cannot
  629. assign to the property directly.
  630. Args:
  631. field: A FieldDescriptor for this field.
  632. cls: The class we're constructing.
  633. """
  634. # TODO: Remove duplication with similar method
  635. # for non-repeated scalars.
  636. proto_field_name = field.name
  637. property_name = _PropertyName(proto_field_name)
  638. def getter(self):
  639. field_value = self._fields.get(field)
  640. if field_value is None:
  641. # Construct a new object to represent this field.
  642. field_value = field._default_constructor(self)
  643. # Atomically check if another thread has preempted us and, if not, swap
  644. # in the new object we just created. If someone has preempted us, we
  645. # take that object and discard ours.
  646. # WARNING: We are relying on setdefault() being atomic. This is true
  647. # in CPython but we haven't investigated others. This warning appears
  648. # in several other locations in this file.
  649. field_value = self._fields.setdefault(field, field_value)
  650. return field_value
  651. getter.__module__ = None
  652. getter.__doc__ = 'Getter for %s.' % proto_field_name
  653. # We define a setter just so we can throw an exception with a more
  654. # helpful error message.
  655. def setter(self, new_value):
  656. if field.message_type.full_name == 'google.protobuf.Timestamp':
  657. getter(self)
  658. self._fields[field].FromDatetime(new_value)
  659. elif field.message_type.full_name == 'google.protobuf.Duration':
  660. getter(self)
  661. self._fields[field].FromTimedelta(new_value)
  662. elif field.message_type.full_name == _StructFullTypeName:
  663. getter(self)
  664. self._fields[field].Clear()
  665. self._fields[field].update(new_value)
  666. elif field.message_type.full_name == _ListValueFullTypeName:
  667. getter(self)
  668. self._fields[field].Clear()
  669. self._fields[field].extend(new_value)
  670. else:
  671. raise AttributeError(
  672. 'Assignment not allowed to composite field '
  673. '"%s" in protocol message object.' % proto_field_name
  674. )
  675. # Add a property to encapsulate the getter.
  676. doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
  677. setattr(cls, property_name, _FieldProperty(field, getter, setter, doc=doc))
  678. def _AddPropertiesForExtensions(descriptor, cls):
  679. """Adds properties for all fields in this protocol message type."""
  680. extensions = descriptor.extensions_by_name
  681. for extension_name, extension_field in extensions.items():
  682. constant_name = extension_name.upper() + '_FIELD_NUMBER'
  683. setattr(cls, constant_name, extension_field.number)
  684. # TODO: Migrate all users of these attributes to functions like
  685. # pool.FindExtensionByNumber(descriptor).
  686. if descriptor.file is not None:
  687. # TODO: Use cls.MESSAGE_FACTORY.pool when available.
  688. pool = descriptor.file.pool
  689. def _AddStaticMethods(cls):
  690. def FromString(s):
  691. message = cls()
  692. message.MergeFromString(s)
  693. return message
  694. cls.FromString = staticmethod(FromString)
  695. def _IsPresent(item):
  696. """Given a (FieldDescriptor, value) tuple from _fields, return true if the
  697. value should be included in the list returned by ListFields()."""
  698. if item[0].label == _FieldDescriptor.LABEL_REPEATED:
  699. return bool(item[1])
  700. elif item[0].cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  701. return item[1]._is_present_in_parent
  702. else:
  703. return True
  704. def _AddListFieldsMethod(message_descriptor, cls):
  705. """Helper for _AddMessageMethods()."""
  706. def ListFields(self):
  707. all_fields = [item for item in self._fields.items() if _IsPresent(item)]
  708. all_fields.sort(key = lambda item: item[0].number)
  709. return all_fields
  710. cls.ListFields = ListFields
  711. def _AddHasFieldMethod(message_descriptor, cls):
  712. """Helper for _AddMessageMethods()."""
  713. hassable_fields = {}
  714. for field in message_descriptor.fields:
  715. if field.label == _FieldDescriptor.LABEL_REPEATED:
  716. continue
  717. # For proto3, only submessages and fields inside a oneof have presence.
  718. if not field.has_presence:
  719. continue
  720. hassable_fields[field.name] = field
  721. # Has methods are supported for oneof descriptors.
  722. for oneof in message_descriptor.oneofs:
  723. hassable_fields[oneof.name] = oneof
  724. def HasField(self, field_name):
  725. try:
  726. field = hassable_fields[field_name]
  727. except KeyError as exc:
  728. raise ValueError('Protocol message %s has no non-repeated field "%s" '
  729. 'nor has presence is not available for this field.' % (
  730. message_descriptor.full_name, field_name)) from exc
  731. if isinstance(field, descriptor_mod.OneofDescriptor):
  732. try:
  733. return HasField(self, self._oneofs[field].name)
  734. except KeyError:
  735. return False
  736. else:
  737. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  738. value = self._fields.get(field)
  739. return value is not None and value._is_present_in_parent
  740. else:
  741. return field in self._fields
  742. cls.HasField = HasField
  743. def _AddClearFieldMethod(message_descriptor, cls):
  744. """Helper for _AddMessageMethods()."""
  745. def ClearField(self, field_name):
  746. try:
  747. field = message_descriptor.fields_by_name[field_name]
  748. except KeyError:
  749. try:
  750. field = message_descriptor.oneofs_by_name[field_name]
  751. if field in self._oneofs:
  752. field = self._oneofs[field]
  753. else:
  754. return
  755. except KeyError:
  756. raise ValueError('Protocol message %s has no "%s" field.' %
  757. (message_descriptor.name, field_name))
  758. if field in self._fields:
  759. # To match the C++ implementation, we need to invalidate iterators
  760. # for map fields when ClearField() happens.
  761. if hasattr(self._fields[field], 'InvalidateIterators'):
  762. self._fields[field].InvalidateIterators()
  763. # Note: If the field is a sub-message, its listener will still point
  764. # at us. That's fine, because the worst than can happen is that it
  765. # will call _Modified() and invalidate our byte size. Big deal.
  766. del self._fields[field]
  767. if self._oneofs.get(field.containing_oneof, None) is field:
  768. del self._oneofs[field.containing_oneof]
  769. # Always call _Modified() -- even if nothing was changed, this is
  770. # a mutating method, and thus calling it should cause the field to become
  771. # present in the parent message.
  772. self._Modified()
  773. cls.ClearField = ClearField
  774. def _AddClearExtensionMethod(cls):
  775. """Helper for _AddMessageMethods()."""
  776. def ClearExtension(self, field_descriptor):
  777. extension_dict._VerifyExtensionHandle(self, field_descriptor)
  778. # Similar to ClearField(), above.
  779. if field_descriptor in self._fields:
  780. del self._fields[field_descriptor]
  781. self._Modified()
  782. cls.ClearExtension = ClearExtension
  783. def _AddHasExtensionMethod(cls):
  784. """Helper for _AddMessageMethods()."""
  785. def HasExtension(self, field_descriptor):
  786. extension_dict._VerifyExtensionHandle(self, field_descriptor)
  787. if field_descriptor.label == _FieldDescriptor.LABEL_REPEATED:
  788. raise KeyError('"%s" is repeated.' % field_descriptor.full_name)
  789. if field_descriptor.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  790. value = self._fields.get(field_descriptor)
  791. return value is not None and value._is_present_in_parent
  792. else:
  793. return field_descriptor in self._fields
  794. cls.HasExtension = HasExtension
  795. def _InternalUnpackAny(msg):
  796. """Unpacks Any message and returns the unpacked message.
  797. This internal method is different from public Any Unpack method which takes
  798. the target message as argument. _InternalUnpackAny method does not have
  799. target message type and need to find the message type in descriptor pool.
  800. Args:
  801. msg: An Any message to be unpacked.
  802. Returns:
  803. The unpacked message.
  804. """
  805. # TODO: Don't use the factory of generated messages.
  806. # To make Any work with custom factories, use the message factory of the
  807. # parent message.
  808. # pylint: disable=g-import-not-at-top
  809. from google.protobuf import symbol_database
  810. factory = symbol_database.Default()
  811. type_url = msg.type_url
  812. if not type_url:
  813. return None
  814. # TODO: For now we just strip the hostname. Better logic will be
  815. # required.
  816. type_name = type_url.split('/')[-1]
  817. descriptor = factory.pool.FindMessageTypeByName(type_name)
  818. if descriptor is None:
  819. return None
  820. # Unable to import message_factory at top because of circular import.
  821. # pylint: disable=g-import-not-at-top
  822. from google.protobuf import message_factory
  823. message_class = message_factory.GetMessageClass(descriptor)
  824. message = message_class()
  825. message.ParseFromString(msg.value)
  826. return message
  827. def _AddEqualsMethod(message_descriptor, cls):
  828. """Helper for _AddMessageMethods()."""
  829. def __eq__(self, other):
  830. if self.DESCRIPTOR.full_name == _ListValueFullTypeName and isinstance(
  831. other, list
  832. ):
  833. return self._internal_compare(other)
  834. if self.DESCRIPTOR.full_name == _StructFullTypeName and isinstance(
  835. other, dict
  836. ):
  837. return self._internal_compare(other)
  838. if (not isinstance(other, message_mod.Message) or
  839. other.DESCRIPTOR != self.DESCRIPTOR):
  840. return NotImplemented
  841. if self is other:
  842. return True
  843. if self.DESCRIPTOR.full_name == _AnyFullTypeName:
  844. any_a = _InternalUnpackAny(self)
  845. any_b = _InternalUnpackAny(other)
  846. if any_a and any_b:
  847. return any_a == any_b
  848. if not self.ListFields() == other.ListFields():
  849. return False
  850. # TODO: Fix UnknownFieldSet to consider MessageSet extensions,
  851. # then use it for the comparison.
  852. unknown_fields = list(self._unknown_fields)
  853. unknown_fields.sort()
  854. other_unknown_fields = list(other._unknown_fields)
  855. other_unknown_fields.sort()
  856. return unknown_fields == other_unknown_fields
  857. cls.__eq__ = __eq__
  858. def _AddStrMethod(message_descriptor, cls):
  859. """Helper for _AddMessageMethods()."""
  860. def __str__(self):
  861. return text_format.MessageToString(self)
  862. cls.__str__ = __str__
  863. def _AddReprMethod(message_descriptor, cls):
  864. """Helper for _AddMessageMethods()."""
  865. def __repr__(self):
  866. return text_format.MessageToString(self)
  867. cls.__repr__ = __repr__
  868. def _AddUnicodeMethod(unused_message_descriptor, cls):
  869. """Helper for _AddMessageMethods()."""
  870. def __unicode__(self):
  871. return text_format.MessageToString(self, as_utf8=True).decode('utf-8')
  872. cls.__unicode__ = __unicode__
  873. def _AddContainsMethod(message_descriptor, cls):
  874. if message_descriptor.full_name == 'google.protobuf.Struct':
  875. def __contains__(self, key):
  876. return key in self.fields
  877. elif message_descriptor.full_name == 'google.protobuf.ListValue':
  878. def __contains__(self, value):
  879. return value in self.items()
  880. else:
  881. def __contains__(self, field):
  882. return self.HasField(field)
  883. cls.__contains__ = __contains__
  884. def _BytesForNonRepeatedElement(value, field_number, field_type):
  885. """Returns the number of bytes needed to serialize a non-repeated element.
  886. The returned byte count includes space for tag information and any
  887. other additional space associated with serializing value.
  888. Args:
  889. value: Value we're serializing.
  890. field_number: Field number of this value. (Since the field number
  891. is stored as part of a varint-encoded tag, this has an impact
  892. on the total bytes required to serialize the value).
  893. field_type: The type of the field. One of the TYPE_* constants
  894. within FieldDescriptor.
  895. """
  896. try:
  897. fn = type_checkers.TYPE_TO_BYTE_SIZE_FN[field_type]
  898. return fn(field_number, value)
  899. except KeyError:
  900. raise message_mod.EncodeError('Unrecognized field type: %d' % field_type)
  901. def _AddByteSizeMethod(message_descriptor, cls):
  902. """Helper for _AddMessageMethods()."""
  903. def ByteSize(self):
  904. if not self._cached_byte_size_dirty:
  905. return self._cached_byte_size
  906. size = 0
  907. descriptor = self.DESCRIPTOR
  908. if descriptor._is_map_entry:
  909. # Fields of map entry should always be serialized.
  910. key_field = descriptor.fields_by_name['key']
  911. _MaybeAddEncoder(cls, key_field)
  912. size = key_field._sizer(self.key)
  913. value_field = descriptor.fields_by_name['value']
  914. _MaybeAddEncoder(cls, value_field)
  915. size += value_field._sizer(self.value)
  916. else:
  917. for field_descriptor, field_value in self.ListFields():
  918. _MaybeAddEncoder(cls, field_descriptor)
  919. size += field_descriptor._sizer(field_value)
  920. for tag_bytes, value_bytes in self._unknown_fields:
  921. size += len(tag_bytes) + len(value_bytes)
  922. self._cached_byte_size = size
  923. self._cached_byte_size_dirty = False
  924. self._listener_for_children.dirty = False
  925. return size
  926. cls.ByteSize = ByteSize
  927. def _AddSerializeToStringMethod(message_descriptor, cls):
  928. """Helper for _AddMessageMethods()."""
  929. def SerializeToString(self, **kwargs):
  930. # Check if the message has all of its required fields set.
  931. if not self.IsInitialized():
  932. raise message_mod.EncodeError(
  933. 'Message %s is missing required fields: %s' % (
  934. self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
  935. return self.SerializePartialToString(**kwargs)
  936. cls.SerializeToString = SerializeToString
  937. def _AddSerializePartialToStringMethod(message_descriptor, cls):
  938. """Helper for _AddMessageMethods()."""
  939. def SerializePartialToString(self, **kwargs):
  940. out = BytesIO()
  941. self._InternalSerialize(out.write, **kwargs)
  942. return out.getvalue()
  943. cls.SerializePartialToString = SerializePartialToString
  944. def InternalSerialize(self, write_bytes, deterministic=None):
  945. if deterministic is None:
  946. deterministic = (
  947. api_implementation.IsPythonDefaultSerializationDeterministic())
  948. else:
  949. deterministic = bool(deterministic)
  950. descriptor = self.DESCRIPTOR
  951. if descriptor._is_map_entry:
  952. # Fields of map entry should always be serialized.
  953. key_field = descriptor.fields_by_name['key']
  954. _MaybeAddEncoder(cls, key_field)
  955. key_field._encoder(write_bytes, self.key, deterministic)
  956. value_field = descriptor.fields_by_name['value']
  957. _MaybeAddEncoder(cls, value_field)
  958. value_field._encoder(write_bytes, self.value, deterministic)
  959. else:
  960. for field_descriptor, field_value in self.ListFields():
  961. _MaybeAddEncoder(cls, field_descriptor)
  962. field_descriptor._encoder(write_bytes, field_value, deterministic)
  963. for tag_bytes, value_bytes in self._unknown_fields:
  964. write_bytes(tag_bytes)
  965. write_bytes(value_bytes)
  966. cls._InternalSerialize = InternalSerialize
  967. def _AddMergeFromStringMethod(message_descriptor, cls):
  968. """Helper for _AddMessageMethods()."""
  969. def MergeFromString(self, serialized):
  970. serialized = memoryview(serialized)
  971. length = len(serialized)
  972. try:
  973. if self._InternalParse(serialized, 0, length) != length:
  974. # The only reason _InternalParse would return early is if it
  975. # encountered an end-group tag.
  976. raise message_mod.DecodeError('Unexpected end-group tag.')
  977. except (IndexError, TypeError):
  978. # Now ord(buf[p:p+1]) == ord('') gets TypeError.
  979. raise message_mod.DecodeError('Truncated message.')
  980. except struct.error as e:
  981. raise message_mod.DecodeError(e)
  982. return length # Return this for legacy reasons.
  983. cls.MergeFromString = MergeFromString
  984. local_ReadTag = decoder.ReadTag
  985. local_SkipField = decoder.SkipField
  986. fields_by_tag = cls._fields_by_tag
  987. message_set_decoders_by_tag = cls._message_set_decoders_by_tag
  988. def InternalParse(self, buffer, pos, end):
  989. """Create a message from serialized bytes.
  990. Args:
  991. self: Message, instance of the proto message object.
  992. buffer: memoryview of the serialized data.
  993. pos: int, position to start in the serialized data.
  994. end: int, end position of the serialized data.
  995. Returns:
  996. Message object.
  997. """
  998. # Guard against internal misuse, since this function is called internally
  999. # quite extensively, and its easy to accidentally pass bytes.
  1000. assert isinstance(buffer, memoryview)
  1001. self._Modified()
  1002. field_dict = self._fields
  1003. while pos != end:
  1004. (tag_bytes, new_pos) = local_ReadTag(buffer, pos)
  1005. field_decoder, field_des = message_set_decoders_by_tag.get(
  1006. tag_bytes, (None, None)
  1007. )
  1008. if field_decoder:
  1009. pos = field_decoder(buffer, new_pos, end, self, field_dict)
  1010. continue
  1011. field_des, is_packed = fields_by_tag.get(tag_bytes, (None, None))
  1012. if field_des is None:
  1013. if not self._unknown_fields: # pylint: disable=protected-access
  1014. self._unknown_fields = [] # pylint: disable=protected-access
  1015. # pylint: disable=protected-access
  1016. (tag, _) = decoder._DecodeVarint(tag_bytes, 0)
  1017. field_number, wire_type = wire_format.UnpackTag(tag)
  1018. if field_number == 0:
  1019. raise message_mod.DecodeError('Field number 0 is illegal.')
  1020. # TODO: remove old_pos.
  1021. old_pos = new_pos
  1022. (data, new_pos) = decoder._DecodeUnknownField(
  1023. buffer, new_pos, wire_type) # pylint: disable=protected-access
  1024. if new_pos == -1:
  1025. return pos
  1026. # TODO: remove _unknown_fields.
  1027. new_pos = local_SkipField(buffer, old_pos, end, tag_bytes)
  1028. if new_pos == -1:
  1029. return pos
  1030. self._unknown_fields.append(
  1031. (tag_bytes, buffer[old_pos:new_pos].tobytes()))
  1032. pos = new_pos
  1033. else:
  1034. _MaybeAddDecoder(cls, field_des)
  1035. field_decoder = field_des._decoders[is_packed]
  1036. pos = field_decoder(buffer, new_pos, end, self, field_dict)
  1037. if field_des.containing_oneof:
  1038. self._UpdateOneofState(field_des)
  1039. return pos
  1040. cls._InternalParse = InternalParse
  1041. def _AddIsInitializedMethod(message_descriptor, cls):
  1042. """Adds the IsInitialized and FindInitializationError methods to the
  1043. protocol message class."""
  1044. required_fields = [field for field in message_descriptor.fields
  1045. if field.label == _FieldDescriptor.LABEL_REQUIRED]
  1046. def IsInitialized(self, errors=None):
  1047. """Checks if all required fields of a message are set.
  1048. Args:
  1049. errors: A list which, if provided, will be populated with the field
  1050. paths of all missing required fields.
  1051. Returns:
  1052. True iff the specified message has all required fields set.
  1053. """
  1054. # Performance is critical so we avoid HasField() and ListFields().
  1055. for field in required_fields:
  1056. if (field not in self._fields or
  1057. (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
  1058. not self._fields[field]._is_present_in_parent)):
  1059. if errors is not None:
  1060. errors.extend(self.FindInitializationErrors())
  1061. return False
  1062. for field, value in list(self._fields.items()): # dict can change size!
  1063. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  1064. if field.label == _FieldDescriptor.LABEL_REPEATED:
  1065. if (field.message_type._is_map_entry):
  1066. continue
  1067. for element in value:
  1068. if not element.IsInitialized():
  1069. if errors is not None:
  1070. errors.extend(self.FindInitializationErrors())
  1071. return False
  1072. elif value._is_present_in_parent and not value.IsInitialized():
  1073. if errors is not None:
  1074. errors.extend(self.FindInitializationErrors())
  1075. return False
  1076. return True
  1077. cls.IsInitialized = IsInitialized
  1078. def FindInitializationErrors(self):
  1079. """Finds required fields which are not initialized.
  1080. Returns:
  1081. A list of strings. Each string is a path to an uninitialized field from
  1082. the top-level message, e.g. "foo.bar[5].baz".
  1083. """
  1084. errors = [] # simplify things
  1085. for field in required_fields:
  1086. if not self.HasField(field.name):
  1087. errors.append(field.name)
  1088. for field, value in self.ListFields():
  1089. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  1090. if field.is_extension:
  1091. name = '(%s)' % field.full_name
  1092. else:
  1093. name = field.name
  1094. if _IsMapField(field):
  1095. if _IsMessageMapField(field):
  1096. for key in value:
  1097. element = value[key]
  1098. prefix = '%s[%s].' % (name, key)
  1099. sub_errors = element.FindInitializationErrors()
  1100. errors += [prefix + error for error in sub_errors]
  1101. else:
  1102. # ScalarMaps can't have any initialization errors.
  1103. pass
  1104. elif field.label == _FieldDescriptor.LABEL_REPEATED:
  1105. for i in range(len(value)):
  1106. element = value[i]
  1107. prefix = '%s[%d].' % (name, i)
  1108. sub_errors = element.FindInitializationErrors()
  1109. errors += [prefix + error for error in sub_errors]
  1110. else:
  1111. prefix = name + '.'
  1112. sub_errors = value.FindInitializationErrors()
  1113. errors += [prefix + error for error in sub_errors]
  1114. return errors
  1115. cls.FindInitializationErrors = FindInitializationErrors
  1116. def _FullyQualifiedClassName(klass):
  1117. module = klass.__module__
  1118. name = getattr(klass, '__qualname__', klass.__name__)
  1119. if module in (None, 'builtins', '__builtin__'):
  1120. return name
  1121. return module + '.' + name
  1122. def _AddMergeFromMethod(cls):
  1123. LABEL_REPEATED = _FieldDescriptor.LABEL_REPEATED
  1124. CPPTYPE_MESSAGE = _FieldDescriptor.CPPTYPE_MESSAGE
  1125. def MergeFrom(self, msg):
  1126. if not isinstance(msg, cls):
  1127. raise TypeError(
  1128. 'Parameter to MergeFrom() must be instance of same class: '
  1129. 'expected %s got %s.' % (_FullyQualifiedClassName(cls),
  1130. _FullyQualifiedClassName(msg.__class__)))
  1131. assert msg is not self
  1132. self._Modified()
  1133. fields = self._fields
  1134. for field, value in msg._fields.items():
  1135. if field.label == LABEL_REPEATED:
  1136. field_value = fields.get(field)
  1137. if field_value is None:
  1138. # Construct a new object to represent this field.
  1139. field_value = field._default_constructor(self)
  1140. fields[field] = field_value
  1141. field_value.MergeFrom(value)
  1142. elif field.cpp_type == CPPTYPE_MESSAGE:
  1143. if value._is_present_in_parent:
  1144. field_value = fields.get(field)
  1145. if field_value is None:
  1146. # Construct a new object to represent this field.
  1147. field_value = field._default_constructor(self)
  1148. fields[field] = field_value
  1149. field_value.MergeFrom(value)
  1150. else:
  1151. self._fields[field] = value
  1152. if field.containing_oneof:
  1153. self._UpdateOneofState(field)
  1154. if msg._unknown_fields:
  1155. if not self._unknown_fields:
  1156. self._unknown_fields = []
  1157. self._unknown_fields.extend(msg._unknown_fields)
  1158. cls.MergeFrom = MergeFrom
  1159. def _AddWhichOneofMethod(message_descriptor, cls):
  1160. def WhichOneof(self, oneof_name):
  1161. """Returns the name of the currently set field inside a oneof, or None."""
  1162. try:
  1163. field = message_descriptor.oneofs_by_name[oneof_name]
  1164. except KeyError:
  1165. raise ValueError(
  1166. 'Protocol message has no oneof "%s" field.' % oneof_name)
  1167. nested_field = self._oneofs.get(field, None)
  1168. if nested_field is not None and self.HasField(nested_field.name):
  1169. return nested_field.name
  1170. else:
  1171. return None
  1172. cls.WhichOneof = WhichOneof
  1173. def _Clear(self):
  1174. # Clear fields.
  1175. self._fields = {}
  1176. self._unknown_fields = ()
  1177. self._oneofs = {}
  1178. self._Modified()
  1179. def _UnknownFields(self):
  1180. raise NotImplementedError('Please use the add-on feaure '
  1181. 'unknown_fields.UnknownFieldSet(message) in '
  1182. 'unknown_fields.py instead.')
  1183. def _DiscardUnknownFields(self):
  1184. self._unknown_fields = []
  1185. for field, value in self.ListFields():
  1186. if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
  1187. if _IsMapField(field):
  1188. if _IsMessageMapField(field):
  1189. for key in value:
  1190. value[key].DiscardUnknownFields()
  1191. elif field.label == _FieldDescriptor.LABEL_REPEATED:
  1192. for sub_message in value:
  1193. sub_message.DiscardUnknownFields()
  1194. else:
  1195. value.DiscardUnknownFields()
  1196. def _SetListener(self, listener):
  1197. if listener is None:
  1198. self._listener = message_listener_mod.NullMessageListener()
  1199. else:
  1200. self._listener = listener
  1201. def _AddMessageMethods(message_descriptor, cls):
  1202. """Adds implementations of all Message methods to cls."""
  1203. _AddListFieldsMethod(message_descriptor, cls)
  1204. _AddHasFieldMethod(message_descriptor, cls)
  1205. _AddClearFieldMethod(message_descriptor, cls)
  1206. if message_descriptor.is_extendable:
  1207. _AddClearExtensionMethod(cls)
  1208. _AddHasExtensionMethod(cls)
  1209. _AddEqualsMethod(message_descriptor, cls)
  1210. _AddStrMethod(message_descriptor, cls)
  1211. _AddReprMethod(message_descriptor, cls)
  1212. _AddUnicodeMethod(message_descriptor, cls)
  1213. _AddContainsMethod(message_descriptor, cls)
  1214. _AddByteSizeMethod(message_descriptor, cls)
  1215. _AddSerializeToStringMethod(message_descriptor, cls)
  1216. _AddSerializePartialToStringMethod(message_descriptor, cls)
  1217. _AddMergeFromStringMethod(message_descriptor, cls)
  1218. _AddIsInitializedMethod(message_descriptor, cls)
  1219. _AddMergeFromMethod(cls)
  1220. _AddWhichOneofMethod(message_descriptor, cls)
  1221. # Adds methods which do not depend on cls.
  1222. cls.Clear = _Clear
  1223. cls.DiscardUnknownFields = _DiscardUnknownFields
  1224. cls._SetListener = _SetListener
  1225. def _AddPrivateHelperMethods(message_descriptor, cls):
  1226. """Adds implementation of private helper methods to cls."""
  1227. def Modified(self):
  1228. """Sets the _cached_byte_size_dirty bit to true,
  1229. and propagates this to our listener iff this was a state change.
  1230. """
  1231. # Note: Some callers check _cached_byte_size_dirty before calling
  1232. # _Modified() as an extra optimization. So, if this method is ever
  1233. # changed such that it does stuff even when _cached_byte_size_dirty is
  1234. # already true, the callers need to be updated.
  1235. if not self._cached_byte_size_dirty:
  1236. self._cached_byte_size_dirty = True
  1237. self._listener_for_children.dirty = True
  1238. self._is_present_in_parent = True
  1239. self._listener.Modified()
  1240. def _UpdateOneofState(self, field):
  1241. """Sets field as the active field in its containing oneof.
  1242. Will also delete currently active field in the oneof, if it is different
  1243. from the argument. Does not mark the message as modified.
  1244. """
  1245. other_field = self._oneofs.setdefault(field.containing_oneof, field)
  1246. if other_field is not field:
  1247. del self._fields[other_field]
  1248. self._oneofs[field.containing_oneof] = field
  1249. cls._Modified = Modified
  1250. cls.SetInParent = Modified
  1251. cls._UpdateOneofState = _UpdateOneofState
  1252. class _Listener(object):
  1253. """MessageListener implementation that a parent message registers with its
  1254. child message.
  1255. In order to support semantics like:
  1256. foo.bar.baz.moo = 23
  1257. assert foo.HasField('bar')
  1258. ...child objects must have back references to their parents.
  1259. This helper class is at the heart of this support.
  1260. """
  1261. def __init__(self, parent_message):
  1262. """Args:
  1263. parent_message: The message whose _Modified() method we should call when
  1264. we receive Modified() messages.
  1265. """
  1266. # This listener establishes a back reference from a child (contained) object
  1267. # to its parent (containing) object. We make this a weak reference to avoid
  1268. # creating cyclic garbage when the client finishes with the 'parent' object
  1269. # in the tree.
  1270. if isinstance(parent_message, weakref.ProxyType):
  1271. self._parent_message_weakref = parent_message
  1272. else:
  1273. self._parent_message_weakref = weakref.proxy(parent_message)
  1274. # As an optimization, we also indicate directly on the listener whether
  1275. # or not the parent message is dirty. This way we can avoid traversing
  1276. # up the tree in the common case.
  1277. self.dirty = False
  1278. def Modified(self):
  1279. if self.dirty:
  1280. return
  1281. try:
  1282. # Propagate the signal to our parents iff this is the first field set.
  1283. self._parent_message_weakref._Modified()
  1284. except ReferenceError:
  1285. # We can get here if a client has kept a reference to a child object,
  1286. # and is now setting a field on it, but the child's parent has been
  1287. # garbage-collected. This is not an error.
  1288. pass
  1289. class _OneofListener(_Listener):
  1290. """Special listener implementation for setting composite oneof fields."""
  1291. def __init__(self, parent_message, field):
  1292. """Args:
  1293. parent_message: The message whose _Modified() method we should call when
  1294. we receive Modified() messages.
  1295. field: The descriptor of the field being set in the parent message.
  1296. """
  1297. super(_OneofListener, self).__init__(parent_message)
  1298. self._field = field
  1299. def Modified(self):
  1300. """Also updates the state of the containing oneof in the parent message."""
  1301. try:
  1302. self._parent_message_weakref._UpdateOneofState(self._field)
  1303. super(_OneofListener, self).Modified()
  1304. except ReferenceError:
  1305. pass