Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: recipe_engine/third_party/google/protobuf/internal/python_message.py

Issue 2236673002: Bump vendoring, move to proto3 release. (Closed) Base URL: https://github.com/luci/recipes-py@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Protocol Buffers - Google's data interchange format 1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc. All rights reserved. 2 # Copyright 2008 Google Inc. All rights reserved.
3 # https://developers.google.com/protocol-buffers/ 3 # https://developers.google.com/protocol-buffers/
4 # 4 #
5 # Redistribution and use in source and binary forms, with or without 5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are 6 # modification, are permitted provided that the following conditions are
7 # met: 7 # met:
8 # 8 #
9 # * Redistributions of source code must retain the above copyright 9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer. 10 # notice, this list of conditions and the following disclaimer.
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 from google.protobuf.internal import containers 69 from google.protobuf.internal import containers
70 from google.protobuf.internal import decoder 70 from google.protobuf.internal import decoder
71 from google.protobuf.internal import encoder 71 from google.protobuf.internal import encoder
72 from google.protobuf.internal import enum_type_wrapper 72 from google.protobuf.internal import enum_type_wrapper
73 from google.protobuf.internal import message_listener as message_listener_mod 73 from google.protobuf.internal import message_listener as message_listener_mod
74 from google.protobuf.internal import type_checkers 74 from google.protobuf.internal import type_checkers
75 from google.protobuf.internal import well_known_types 75 from google.protobuf.internal import well_known_types
76 from google.protobuf.internal import wire_format 76 from google.protobuf.internal import wire_format
77 from google.protobuf import descriptor as descriptor_mod 77 from google.protobuf import descriptor as descriptor_mod
78 from google.protobuf import message as message_mod 78 from google.protobuf import message as message_mod
79 from google.protobuf import symbol_database
80 from google.protobuf import text_format 79 from google.protobuf import text_format
81 80
82 _FieldDescriptor = descriptor_mod.FieldDescriptor 81 _FieldDescriptor = descriptor_mod.FieldDescriptor
83 _AnyFullTypeName = 'google.protobuf.Any' 82 _AnyFullTypeName = 'google.protobuf.Any'
84 83
85 84
86 class GeneratedProtocolMessageType(type): 85 class GeneratedProtocolMessageType(type):
87 86
88 """Metaclass for protocol message classes created at runtime from Descriptors. 87 """Metaclass for protocol message classes created at runtime from Descriptors.
89 88
90 We add implementations for all methods described in the Message class. We 89 We add implementations for all methods described in the Message class. We
91 also create properties to allow getting/setting all fields in the protocol 90 also create properties to allow getting/setting all fields in the protocol
92 message. Finally, we create slots to prevent users from accidentally 91 message. Finally, we create slots to prevent users from accidentally
93 "setting" nonexistent fields in the protocol message, which then wouldn't get 92 "setting" nonexistent fields in the protocol message, which then wouldn't get
94 serialized / deserialized properly. 93 serialized / deserialized properly.
95 94
96 The protocol compiler currently uses this metaclass to create protocol 95 The protocol compiler currently uses this metaclass to create protocol
97 message classes at runtime. Clients can also manually create their own 96 message classes at runtime. Clients can also manually create their own
98 classes at runtime, as in this example: 97 classes at runtime, as in this example:
99 98
100 mydescriptor = Descriptor(.....) 99 mydescriptor = Descriptor(.....)
101 class MyProtoClass(Message): 100 factory = symbol_database.Default()
102 __metaclass__ = GeneratedProtocolMessageType 101 factory.pool.AddDescriptor(mydescriptor)
103 DESCRIPTOR = mydescriptor 102 MyProtoClass = factory.GetPrototype(mydescriptor)
104 myproto_instance = MyProtoClass() 103 myproto_instance = MyProtoClass()
105 myproto.foo_field = 23 104 myproto.foo_field = 23
106 ... 105 ...
107
108 The above example will not work for nested types. If you wish to include them,
109 use reflection.MakeClass() instead of manually instantiating the class in
110 order to create the appropriate class structure.
111 """ 106 """
112 107
113 # Must be consistent with the protocol-compiler code in 108 # Must be consistent with the protocol-compiler code in
114 # proto2/compiler/internal/generator.*. 109 # proto2/compiler/internal/generator.*.
115 _DESCRIPTOR_KEY = 'DESCRIPTOR' 110 _DESCRIPTOR_KEY = 'DESCRIPTOR'
116 111
117 def __new__(cls, name, bases, dictionary): 112 def __new__(cls, name, bases, dictionary):
118 """Custom allocation for runtime-generated class types. 113 """Custom allocation for runtime-generated class types.
119 114
120 We override __new__ because this is apparently the only place 115 We override __new__ because this is apparently the only place
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after
919 This internal method is differnt from public Any Unpack method which takes 914 This internal method is differnt from public Any Unpack method which takes
920 the target message as argument. _InternalUnpackAny method does not have 915 the target message as argument. _InternalUnpackAny method does not have
921 target message type and need to find the message type in descriptor pool. 916 target message type and need to find the message type in descriptor pool.
922 917
923 Args: 918 Args:
924 msg: An Any message to be unpacked. 919 msg: An Any message to be unpacked.
925 920
926 Returns: 921 Returns:
927 The unpacked message. 922 The unpacked message.
928 """ 923 """
924 # TODO(amauryfa): Don't use the factory of generated messages.
925 # To make Any work with custom factories, use the message factory of the
926 # parent message.
927 # pylint: disable=g-import-not-at-top
928 from google.protobuf import symbol_database
929 factory = symbol_database.Default()
930
929 type_url = msg.type_url 931 type_url = msg.type_url
930 db = symbol_database.Default()
931 932
932 if not type_url: 933 if not type_url:
933 return None 934 return None
934 935
935 # TODO(haberman): For now we just strip the hostname. Better logic will be 936 # TODO(haberman): For now we just strip the hostname. Better logic will be
936 # required. 937 # required.
937 type_name = type_url.split("/")[-1] 938 type_name = type_url.split('/')[-1]
938 descriptor = db.pool.FindMessageTypeByName(type_name) 939 descriptor = factory.pool.FindMessageTypeByName(type_name)
939 940
940 if descriptor is None: 941 if descriptor is None:
941 return None 942 return None
942 943
943 message_class = db.GetPrototype(descriptor) 944 message_class = factory.GetPrototype(descriptor)
944 message = message_class() 945 message = message_class()
945 946
946 message.ParseFromString(msg.value) 947 message.ParseFromString(msg.value)
947 return message 948 return message
948 949
950
949 def _AddEqualsMethod(message_descriptor, cls): 951 def _AddEqualsMethod(message_descriptor, cls):
950 """Helper for _AddMessageMethods().""" 952 """Helper for _AddMessageMethods()."""
951 def __eq__(self, other): 953 def __eq__(self, other):
952 if (not isinstance(other, message_mod.Message) or 954 if (not isinstance(other, message_mod.Message) or
953 other.DESCRIPTOR != self.DESCRIPTOR): 955 other.DESCRIPTOR != self.DESCRIPTOR):
954 return False 956 return False
955 957
956 if self is other: 958 if self is other:
957 return True 959 return True
958 960
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after
1539 def _FindExtensionByNumber(self, number): 1541 def _FindExtensionByNumber(self, number):
1540 """Tries to find a known extension with the field number. 1542 """Tries to find a known extension with the field number.
1541 1543
1542 Args: 1544 Args:
1543 number: Extension field number. 1545 number: Extension field number.
1544 1546
1545 Returns: 1547 Returns:
1546 Extension field descriptor. 1548 Extension field descriptor.
1547 """ 1549 """
1548 return self._extended_message._extensions_by_number.get(number, None) 1550 return self._extended_message._extensions_by_number.get(number, None)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698