店播爬取Python脚本

python_message.py 57KB

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