reflection.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. # This code is meant to work on Python 2.4 and above only.
  8. """Contains a metaclass and helper functions used to create
  9. protocol message classes from Descriptor objects at runtime.
  10. Recall that a metaclass is the "type" of a class.
  11. (A class is to a metaclass what an instance is to a class.)
  12. In this case, we use the GeneratedProtocolMessageType metaclass
  13. to inject all the useful functionality into the classes
  14. output by the protocol compiler at compile-time.
  15. The upshot of all this is that the real implementation
  16. details for ALL pure-Python protocol buffers are *here in
  17. this file*.
  18. """
  19. __author__ = 'robinson@google.com (Will Robinson)'
  20. import warnings
  21. from google.protobuf import message_factory
  22. from google.protobuf import symbol_database
  23. # The type of all Message classes.
  24. # Part of the public interface, but normally only used by message factories.
  25. GeneratedProtocolMessageType = message_factory._GENERATED_PROTOCOL_MESSAGE_TYPE
  26. MESSAGE_CLASS_CACHE = {}
  27. # Deprecated. Please NEVER use reflection.ParseMessage().
  28. def ParseMessage(descriptor, byte_str):
  29. """Generate a new Message instance from this Descriptor and a byte string.
  30. DEPRECATED: ParseMessage is deprecated because it is using MakeClass().
  31. Please use MessageFactory.GetMessageClass() instead.
  32. Args:
  33. descriptor: Protobuf Descriptor object
  34. byte_str: Serialized protocol buffer byte string
  35. Returns:
  36. Newly created protobuf Message object.
  37. """
  38. warnings.warn(
  39. 'reflection.ParseMessage() is deprecated. Please use '
  40. 'MessageFactory.GetMessageClass() and message.ParseFromString() instead. '
  41. 'reflection.ParseMessage() will be removed in Jan 2025.',
  42. stacklevel=2,
  43. )
  44. result_class = MakeClass(descriptor)
  45. new_msg = result_class()
  46. new_msg.ParseFromString(byte_str)
  47. return new_msg
  48. # Deprecated. Please NEVER use reflection.MakeClass().
  49. def MakeClass(descriptor):
  50. """Construct a class object for a protobuf described by descriptor.
  51. DEPRECATED: use MessageFactory.GetMessageClass() instead.
  52. Args:
  53. descriptor: A descriptor.Descriptor object describing the protobuf.
  54. Returns:
  55. The Message class object described by the descriptor.
  56. """
  57. warnings.warn(
  58. 'reflection.MakeClass() is deprecated. Please use '
  59. 'MessageFactory.GetMessageClass() instead. '
  60. 'reflection.MakeClass() will be removed in Jan 2025.',
  61. stacklevel=2,
  62. )
  63. # Original implementation leads to duplicate message classes, which won't play
  64. # well with extensions. Message factory info is also missing.
  65. # Redirect to message_factory.
  66. return message_factory.GetMessageClass(descriptor)