python_message.py 56 KB

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