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
8"""Descriptors essentially contain exactly the information found in a .proto
9file, in types that make this information accessible in Python.
10"""
11
12__author__ = 'robinson@google.com (Will Robinson)'
13
14import abc
15import binascii
16import os
17import threading
18import warnings
19
20from google.protobuf.internal import api_implementation
21
22_USE_C_DESCRIPTORS = False
23if api_implementation.Type() != 'python':
24 # pylint: disable=protected-access
25 _message = api_implementation._c_module
26 # TODO: Remove this import after fix api_implementation
27 if _message is None:
28 from google.protobuf.pyext import _message
29 _USE_C_DESCRIPTORS = True
30
31
32class Error(Exception):
33 """Base error for this module."""
34
35
36class TypeTransformationError(Error):
37 """Error transforming between python proto type and corresponding C++ type."""
38
39
40if _USE_C_DESCRIPTORS:
41 # This metaclass allows to override the behavior of code like
42 # isinstance(my_descriptor, FieldDescriptor)
43 # and make it return True when the descriptor is an instance of the extension
44 # type written in C++.
45 class DescriptorMetaclass(type):
46
47 def __instancecheck__(cls, obj):
48 if super(DescriptorMetaclass, cls).__instancecheck__(obj):
49 return True
50 if isinstance(obj, cls._C_DESCRIPTOR_CLASS):
51 return True
52 return False
53else:
54 # The standard metaclass; nothing changes.
55 DescriptorMetaclass = abc.ABCMeta
56
57
58class _Lock(object):
59 """Wrapper class of threading.Lock(), which is allowed by 'with'."""
60
61 def __new__(cls):
62 self = object.__new__(cls)
63 self._lock = threading.Lock() # pylint: disable=protected-access
64 return self
65
66 def __enter__(self):
67 self._lock.acquire()
68
69 def __exit__(self, exc_type, exc_value, exc_tb):
70 self._lock.release()
71
72
73_lock = threading.Lock()
74
75
76def _Deprecated(name):
77 if _Deprecated.count > 0:
78 _Deprecated.count -= 1
79 warnings.warn(
80 'Call to deprecated create function %s(). Note: Create unlinked '
81 'descriptors is going to go away. Please use get/find descriptors from '
82 'generated code or query the descriptor_pool.'
83 % name,
84 category=DeprecationWarning, stacklevel=3)
85
86# These must match the values in descriptor.proto, but we can't use them
87# directly because we sometimes need to reference them in feature helpers
88# below *during* the build of descriptor.proto.
89_FEATURESET_MESSAGE_ENCODING_DELIMITED = 2
90_FEATURESET_FIELD_PRESENCE_IMPLICIT = 2
91_FEATURESET_FIELD_PRESENCE_LEGACY_REQUIRED = 3
92_FEATURESET_REPEATED_FIELD_ENCODING_PACKED = 1
93_FEATURESET_ENUM_TYPE_CLOSED = 2
94
95# Deprecated warnings will print 100 times at most which should be enough for
96# users to notice and do not cause timeout.
97_Deprecated.count = 100
98
99
100_internal_create_key = object()
101
102
103class DescriptorBase(metaclass=DescriptorMetaclass):
104
105 """Descriptors base class.
106
107 This class is the base of all descriptor classes. It provides common options
108 related functionality.
109
110 Attributes:
111 has_options: True if the descriptor has non-default options. Usually it is
112 not necessary to read this -- just call GetOptions() which will happily
113 return the default instance. However, it's sometimes useful for
114 efficiency, and also useful inside the protobuf implementation to avoid
115 some bootstrapping issues.
116 file (FileDescriptor): Reference to file info.
117 """
118
119 if _USE_C_DESCRIPTORS:
120 # The class, or tuple of classes, that are considered as "virtual
121 # subclasses" of this descriptor class.
122 _C_DESCRIPTOR_CLASS = ()
123
124 def __init__(self, file, options, serialized_options, options_class_name):
125 """Initialize the descriptor given its options message and the name of the
126 class of the options message. The name of the class is required in case
127 the options message is None and has to be created.
128 """
129 self._features = None
130 self.file = file
131 self._options = options
132 self._loaded_options = None
133 self._options_class_name = options_class_name
134 self._serialized_options = serialized_options
135
136 # Does this descriptor have non-default options?
137 self.has_options = (self._options is not None) or (
138 self._serialized_options is not None
139 )
140
141 @property
142 @abc.abstractmethod
143 def _parent(self):
144 pass
145
146 def _InferLegacyFeatures(self, edition, options, features):
147 """Infers features from proto2/proto3 syntax so that editions logic can be used everywhere.
148
149 Args:
150 edition: The edition to infer features for.
151 options: The options for this descriptor that are being processed.
152 features: The feature set object to modify with inferred features.
153 """
154 pass
155
156 def _GetFeatures(self):
157 if not self._features:
158 self._LazyLoadOptions()
159 return self._features
160
161 def _ResolveFeatures(self, edition, raw_options):
162 """Resolves features from the raw options of this descriptor.
163
164 Args:
165 edition: The edition to use for feature defaults.
166 raw_options: The options for this descriptor that are being processed.
167
168 Returns:
169 A fully resolved feature set for making runtime decisions.
170 """
171 # pylint: disable=g-import-not-at-top
172 from google.protobuf import descriptor_pb2
173
174 if self._parent:
175 features = descriptor_pb2.FeatureSet()
176 features.CopyFrom(self._parent._GetFeatures())
177 else:
178 features = self.file.pool._CreateDefaultFeatures(edition)
179 unresolved = descriptor_pb2.FeatureSet()
180 unresolved.CopyFrom(raw_options.features)
181 self._InferLegacyFeatures(edition, raw_options, unresolved)
182 features.MergeFrom(unresolved)
183
184 # Use the feature cache to reduce memory bloat.
185 return self.file.pool._InternFeatures(features)
186
187 def _LazyLoadOptions(self):
188 """Lazily initializes descriptor options towards the end of the build."""
189 if self._loaded_options:
190 return
191
192 # pylint: disable=g-import-not-at-top
193 from google.protobuf import descriptor_pb2
194
195 if not hasattr(descriptor_pb2, self._options_class_name):
196 raise RuntimeError(
197 'Unknown options class name %s!' % self._options_class_name
198 )
199 options_class = getattr(descriptor_pb2, self._options_class_name)
200 features = None
201 edition = self.file._edition
202
203 if not self.has_options:
204 if not self._features:
205 features = self._ResolveFeatures(
206 descriptor_pb2.Edition.Value(edition), options_class()
207 )
208 with _lock:
209 self._loaded_options = options_class()
210 if not self._features:
211 self._features = features
212 else:
213 if not self._serialized_options:
214 options = self._options
215 else:
216 options = _ParseOptions(options_class(), self._serialized_options)
217
218 if not self._features:
219 features = self._ResolveFeatures(
220 descriptor_pb2.Edition.Value(edition), options
221 )
222 with _lock:
223 self._loaded_options = options
224 if not self._features:
225 self._features = features
226 if options.HasField('features'):
227 options.ClearField('features')
228 if not options.SerializeToString():
229 self._loaded_options = options_class()
230 self.has_options = False
231
232 def GetOptions(self):
233 """Retrieves descriptor options.
234
235 Returns:
236 The options set on this descriptor.
237 """
238 if not self._loaded_options:
239 self._LazyLoadOptions()
240 return self._loaded_options
241
242
243class _NestedDescriptorBase(DescriptorBase):
244 """Common class for descriptors that can be nested."""
245
246 def __init__(self, options, options_class_name, name, full_name,
247 file, containing_type, serialized_start=None,
248 serialized_end=None, serialized_options=None):
249 """Constructor.
250
251 Args:
252 options: Protocol message options or None to use default message options.
253 options_class_name (str): The class name of the above options.
254 name (str): Name of this protocol message type.
255 full_name (str): Fully-qualified name of this protocol message type, which
256 will include protocol "package" name and the name of any enclosing
257 types.
258 containing_type: if provided, this is a nested descriptor, with this
259 descriptor as parent, otherwise None.
260 serialized_start: The start index (inclusive) in block in the
261 file.serialized_pb that describes this descriptor.
262 serialized_end: The end index (exclusive) in block in the
263 file.serialized_pb that describes this descriptor.
264 serialized_options: Protocol message serialized options or None.
265 """
266 super(_NestedDescriptorBase, self).__init__(
267 file, options, serialized_options, options_class_name
268 )
269
270 self.name = name
271 # TODO: Add function to calculate full_name instead of having it in
272 # memory?
273 self.full_name = full_name
274 self.containing_type = containing_type
275
276 self._serialized_start = serialized_start
277 self._serialized_end = serialized_end
278
279 def CopyToProto(self, proto):
280 """Copies this to the matching proto in descriptor_pb2.
281
282 Args:
283 proto: An empty proto instance from descriptor_pb2.
284
285 Raises:
286 Error: If self couldn't be serialized, due to to few constructor
287 arguments.
288 """
289 if (self.file is not None and
290 self._serialized_start is not None and
291 self._serialized_end is not None):
292 proto.ParseFromString(self.file.serialized_pb[
293 self._serialized_start:self._serialized_end])
294 else:
295 raise Error('Descriptor does not contain serialization.')
296
297
298class Descriptor(_NestedDescriptorBase):
299
300 """Descriptor for a protocol message type.
301
302 Attributes:
303 name (str): Name of this protocol message type.
304 full_name (str): Fully-qualified name of this protocol message type,
305 which will include protocol "package" name and the name of any
306 enclosing types.
307 containing_type (Descriptor): Reference to the descriptor of the type
308 containing us, or None if this is top-level.
309 fields (list[FieldDescriptor]): Field descriptors for all fields in
310 this type.
311 fields_by_number (dict(int, FieldDescriptor)): Same
312 :class:`FieldDescriptor` objects as in :attr:`fields`, but indexed
313 by "number" attribute in each FieldDescriptor.
314 fields_by_name (dict(str, FieldDescriptor)): Same
315 :class:`FieldDescriptor` objects as in :attr:`fields`, but indexed by
316 "name" attribute in each :class:`FieldDescriptor`.
317 nested_types (list[Descriptor]): Descriptor references
318 for all protocol message types nested within this one.
319 nested_types_by_name (dict(str, Descriptor)): Same Descriptor
320 objects as in :attr:`nested_types`, but indexed by "name" attribute
321 in each Descriptor.
322 enum_types (list[EnumDescriptor]): :class:`EnumDescriptor` references
323 for all enums contained within this type.
324 enum_types_by_name (dict(str, EnumDescriptor)): Same
325 :class:`EnumDescriptor` objects as in :attr:`enum_types`, but
326 indexed by "name" attribute in each EnumDescriptor.
327 enum_values_by_name (dict(str, EnumValueDescriptor)): Dict mapping
328 from enum value name to :class:`EnumValueDescriptor` for that value.
329 extensions (list[FieldDescriptor]): All extensions defined directly
330 within this message type (NOT within a nested type).
331 extensions_by_name (dict(str, FieldDescriptor)): Same FieldDescriptor
332 objects as :attr:`extensions`, but indexed by "name" attribute of each
333 FieldDescriptor.
334 is_extendable (bool): Does this type define any extension ranges?
335 oneofs (list[OneofDescriptor]): The list of descriptors for oneof fields
336 in this message.
337 oneofs_by_name (dict(str, OneofDescriptor)): Same objects as in
338 :attr:`oneofs`, but indexed by "name" attribute.
339 file (FileDescriptor): Reference to file descriptor.
340 is_map_entry: If the message type is a map entry.
341
342 """
343
344 if _USE_C_DESCRIPTORS:
345 _C_DESCRIPTOR_CLASS = _message.Descriptor
346
347 def __new__(
348 cls,
349 name=None,
350 full_name=None,
351 filename=None,
352 containing_type=None,
353 fields=None,
354 nested_types=None,
355 enum_types=None,
356 extensions=None,
357 options=None,
358 serialized_options=None,
359 is_extendable=True,
360 extension_ranges=None,
361 oneofs=None,
362 file=None, # pylint: disable=redefined-builtin
363 serialized_start=None,
364 serialized_end=None,
365 syntax=None,
366 is_map_entry=False,
367 create_key=None):
368 _message.Message._CheckCalledFromGeneratedFile()
369 return _message.default_pool.FindMessageTypeByName(full_name)
370
371 # NOTE: The file argument redefining a builtin is nothing we can
372 # fix right now since we don't know how many clients already rely on the
373 # name of the argument.
374 def __init__(self, name, full_name, filename, containing_type, fields,
375 nested_types, enum_types, extensions, options=None,
376 serialized_options=None,
377 is_extendable=True, extension_ranges=None, oneofs=None,
378 file=None, serialized_start=None, serialized_end=None, # pylint: disable=redefined-builtin
379 syntax=None, is_map_entry=False, create_key=None):
380 """Arguments to __init__() are as described in the description
381 of Descriptor fields above.
382
383 Note that filename is an obsolete argument, that is not used anymore.
384 Please use file.name to access this as an attribute.
385 """
386 if create_key is not _internal_create_key:
387 _Deprecated('Descriptor')
388
389 super(Descriptor, self).__init__(
390 options, 'MessageOptions', name, full_name, file,
391 containing_type, serialized_start=serialized_start,
392 serialized_end=serialized_end, serialized_options=serialized_options)
393
394 # We have fields in addition to fields_by_name and fields_by_number,
395 # so that:
396 # 1. Clients can index fields by "order in which they're listed."
397 # 2. Clients can easily iterate over all fields with the terse
398 # syntax: for f in descriptor.fields: ...
399 self.fields = fields
400 for field in self.fields:
401 field.containing_type = self
402 field.file = file
403 self.fields_by_number = dict((f.number, f) for f in fields)
404 self.fields_by_name = dict((f.name, f) for f in fields)
405 self._fields_by_camelcase_name = None
406
407 self.nested_types = nested_types
408 for nested_type in nested_types:
409 nested_type.containing_type = self
410 self.nested_types_by_name = dict((t.name, t) for t in nested_types)
411
412 self.enum_types = enum_types
413 for enum_type in self.enum_types:
414 enum_type.containing_type = self
415 self.enum_types_by_name = dict((t.name, t) for t in enum_types)
416 self.enum_values_by_name = dict(
417 (v.name, v) for t in enum_types for v in t.values)
418
419 self.extensions = extensions
420 for extension in self.extensions:
421 extension.extension_scope = self
422 self.extensions_by_name = dict((f.name, f) for f in extensions)
423 self.is_extendable = is_extendable
424 self.extension_ranges = extension_ranges
425 self.oneofs = oneofs if oneofs is not None else []
426 self.oneofs_by_name = dict((o.name, o) for o in self.oneofs)
427 for oneof in self.oneofs:
428 oneof.containing_type = self
429 oneof.file = file
430 self._is_map_entry = is_map_entry
431
432 @property
433 def _parent(self):
434 return self.containing_type or self.file
435
436 @property
437 def fields_by_camelcase_name(self):
438 """Same FieldDescriptor objects as in :attr:`fields`, but indexed by
439 :attr:`FieldDescriptor.camelcase_name`.
440 """
441 if self._fields_by_camelcase_name is None:
442 self._fields_by_camelcase_name = dict(
443 (f.camelcase_name, f) for f in self.fields)
444 return self._fields_by_camelcase_name
445
446 def EnumValueName(self, enum, value):
447 """Returns the string name of an enum value.
448
449 This is just a small helper method to simplify a common operation.
450
451 Args:
452 enum: string name of the Enum.
453 value: int, value of the enum.
454
455 Returns:
456 string name of the enum value.
457
458 Raises:
459 KeyError if either the Enum doesn't exist or the value is not a valid
460 value for the enum.
461 """
462 return self.enum_types_by_name[enum].values_by_number[value].name
463
464 def CopyToProto(self, proto):
465 """Copies this to a descriptor_pb2.DescriptorProto.
466
467 Args:
468 proto: An empty descriptor_pb2.DescriptorProto.
469 """
470 # This function is overridden to give a better doc comment.
471 super(Descriptor, self).CopyToProto(proto)
472
473
474# TODO: We should have aggressive checking here,
475# for example:
476# * If you specify a repeated field, you should not be allowed
477# to specify a default value.
478# * [Other examples here as needed].
479#
480# TODO: for this and other *Descriptor classes, we
481# might also want to lock things down aggressively (e.g.,
482# prevent clients from setting the attributes). Having
483# stronger invariants here in general will reduce the number
484# of runtime checks we must do in reflection.py...
485class FieldDescriptor(DescriptorBase):
486
487 """Descriptor for a single field in a .proto file.
488
489 Attributes:
490 name (str): Name of this field, exactly as it appears in .proto.
491 full_name (str): Name of this field, including containing scope. This is
492 particularly relevant for extensions.
493 index (int): Dense, 0-indexed index giving the order that this
494 field textually appears within its message in the .proto file.
495 number (int): Tag number declared for this field in the .proto file.
496
497 type (int): (One of the TYPE_* constants below) Declared type.
498 cpp_type (int): (One of the CPPTYPE_* constants below) C++ type used to
499 represent this field.
500
501 label (int): (One of the LABEL_* constants below) Tells whether this
502 field is optional, required, or repeated.
503 has_default_value (bool): True if this field has a default value defined,
504 otherwise false.
505 default_value (Varies): Default value of this field. Only
506 meaningful for non-repeated scalar fields. Repeated fields
507 should always set this to [], and non-repeated composite
508 fields should always set this to None.
509
510 containing_type (Descriptor): Descriptor of the protocol message
511 type that contains this field. Set by the Descriptor constructor
512 if we're passed into one.
513 Somewhat confusingly, for extension fields, this is the
514 descriptor of the EXTENDED message, not the descriptor
515 of the message containing this field. (See is_extension and
516 extension_scope below).
517 message_type (Descriptor): If a composite field, a descriptor
518 of the message type contained in this field. Otherwise, this is None.
519 enum_type (EnumDescriptor): If this field contains an enum, a
520 descriptor of that enum. Otherwise, this is None.
521
522 is_extension: True iff this describes an extension field.
523 extension_scope (Descriptor): Only meaningful if is_extension is True.
524 Gives the message that immediately contains this extension field.
525 Will be None iff we're a top-level (file-level) extension field.
526
527 options (descriptor_pb2.FieldOptions): Protocol message field options or
528 None to use default field options.
529
530 containing_oneof (OneofDescriptor): If the field is a member of a oneof
531 union, contains its descriptor. Otherwise, None.
532
533 file (FileDescriptor): Reference to file descriptor.
534 """
535
536 # Must be consistent with C++ FieldDescriptor::Type enum in
537 # descriptor.h.
538 #
539 # TODO: Find a way to eliminate this repetition.
540 TYPE_DOUBLE = 1
541 TYPE_FLOAT = 2
542 TYPE_INT64 = 3
543 TYPE_UINT64 = 4
544 TYPE_INT32 = 5
545 TYPE_FIXED64 = 6
546 TYPE_FIXED32 = 7
547 TYPE_BOOL = 8
548 TYPE_STRING = 9
549 TYPE_GROUP = 10
550 TYPE_MESSAGE = 11
551 TYPE_BYTES = 12
552 TYPE_UINT32 = 13
553 TYPE_ENUM = 14
554 TYPE_SFIXED32 = 15
555 TYPE_SFIXED64 = 16
556 TYPE_SINT32 = 17
557 TYPE_SINT64 = 18
558 MAX_TYPE = 18
559
560 # Must be consistent with C++ FieldDescriptor::CppType enum in
561 # descriptor.h.
562 #
563 # TODO: Find a way to eliminate this repetition.
564 CPPTYPE_INT32 = 1
565 CPPTYPE_INT64 = 2
566 CPPTYPE_UINT32 = 3
567 CPPTYPE_UINT64 = 4
568 CPPTYPE_DOUBLE = 5
569 CPPTYPE_FLOAT = 6
570 CPPTYPE_BOOL = 7
571 CPPTYPE_ENUM = 8
572 CPPTYPE_STRING = 9
573 CPPTYPE_MESSAGE = 10
574 MAX_CPPTYPE = 10
575
576 _PYTHON_TO_CPP_PROTO_TYPE_MAP = {
577 TYPE_DOUBLE: CPPTYPE_DOUBLE,
578 TYPE_FLOAT: CPPTYPE_FLOAT,
579 TYPE_ENUM: CPPTYPE_ENUM,
580 TYPE_INT64: CPPTYPE_INT64,
581 TYPE_SINT64: CPPTYPE_INT64,
582 TYPE_SFIXED64: CPPTYPE_INT64,
583 TYPE_UINT64: CPPTYPE_UINT64,
584 TYPE_FIXED64: CPPTYPE_UINT64,
585 TYPE_INT32: CPPTYPE_INT32,
586 TYPE_SFIXED32: CPPTYPE_INT32,
587 TYPE_SINT32: CPPTYPE_INT32,
588 TYPE_UINT32: CPPTYPE_UINT32,
589 TYPE_FIXED32: CPPTYPE_UINT32,
590 TYPE_BYTES: CPPTYPE_STRING,
591 TYPE_STRING: CPPTYPE_STRING,
592 TYPE_BOOL: CPPTYPE_BOOL,
593 TYPE_MESSAGE: CPPTYPE_MESSAGE,
594 TYPE_GROUP: CPPTYPE_MESSAGE
595 }
596
597 # Must be consistent with C++ FieldDescriptor::Label enum in
598 # descriptor.h.
599 #
600 # TODO: Find a way to eliminate this repetition.
601 LABEL_OPTIONAL = 1
602 LABEL_REQUIRED = 2
603 LABEL_REPEATED = 3
604 MAX_LABEL = 3
605
606 # Must be consistent with C++ constants kMaxNumber, kFirstReservedNumber,
607 # and kLastReservedNumber in descriptor.h
608 MAX_FIELD_NUMBER = (1 << 29) - 1
609 FIRST_RESERVED_FIELD_NUMBER = 19000
610 LAST_RESERVED_FIELD_NUMBER = 19999
611
612 if _USE_C_DESCRIPTORS:
613 _C_DESCRIPTOR_CLASS = _message.FieldDescriptor
614
615 def __new__(cls, name, full_name, index, number, type, cpp_type, label,
616 default_value, message_type, enum_type, containing_type,
617 is_extension, extension_scope, options=None,
618 serialized_options=None,
619 has_default_value=True, containing_oneof=None, json_name=None,
620 file=None, create_key=None): # pylint: disable=redefined-builtin
621 _message.Message._CheckCalledFromGeneratedFile()
622 if is_extension:
623 return _message.default_pool.FindExtensionByName(full_name)
624 else:
625 return _message.default_pool.FindFieldByName(full_name)
626
627 def __init__(self, name, full_name, index, number, type, cpp_type, label,
628 default_value, message_type, enum_type, containing_type,
629 is_extension, extension_scope, options=None,
630 serialized_options=None,
631 has_default_value=True, containing_oneof=None, json_name=None,
632 file=None, create_key=None): # pylint: disable=redefined-builtin
633 """The arguments are as described in the description of FieldDescriptor
634 attributes above.
635
636 Note that containing_type may be None, and may be set later if necessary
637 (to deal with circular references between message types, for example).
638 Likewise for extension_scope.
639 """
640 if create_key is not _internal_create_key:
641 _Deprecated('FieldDescriptor')
642
643 super(FieldDescriptor, self).__init__(
644 file, options, serialized_options, 'FieldOptions'
645 )
646 self.name = name
647 self.full_name = full_name
648 self._camelcase_name = None
649 if json_name is None:
650 self.json_name = _ToJsonName(name)
651 else:
652 self.json_name = json_name
653 self.index = index
654 self.number = number
655 self._type = type
656 self.cpp_type = cpp_type
657 self._label = label
658 self.has_default_value = has_default_value
659 self.default_value = default_value
660 self.containing_type = containing_type
661 self.message_type = message_type
662 self.enum_type = enum_type
663 self.is_extension = is_extension
664 self.extension_scope = extension_scope
665 self.containing_oneof = containing_oneof
666 if api_implementation.Type() == 'python':
667 self._cdescriptor = None
668 else:
669 if is_extension:
670 self._cdescriptor = _message.default_pool.FindExtensionByName(full_name)
671 else:
672 self._cdescriptor = _message.default_pool.FindFieldByName(full_name)
673
674 @property
675 def _parent(self):
676 if self.containing_oneof:
677 return self.containing_oneof
678 if self.is_extension:
679 return self.extension_scope or self.file
680 return self.containing_type
681
682 def _InferLegacyFeatures(self, edition, options, features):
683 # pylint: disable=g-import-not-at-top
684 from google.protobuf import descriptor_pb2
685
686 if edition >= descriptor_pb2.Edition.EDITION_2023:
687 return
688
689 if self._label == FieldDescriptor.LABEL_REQUIRED:
690 features.field_presence = (
691 descriptor_pb2.FeatureSet.FieldPresence.LEGACY_REQUIRED
692 )
693
694 if self._type == FieldDescriptor.TYPE_GROUP:
695 features.message_encoding = (
696 descriptor_pb2.FeatureSet.MessageEncoding.DELIMITED
697 )
698
699 if options.HasField('packed'):
700 features.repeated_field_encoding = (
701 descriptor_pb2.FeatureSet.RepeatedFieldEncoding.PACKED
702 if options.packed
703 else descriptor_pb2.FeatureSet.RepeatedFieldEncoding.EXPANDED
704 )
705
706 @property
707 def type(self):
708 if (
709 self._GetFeatures().message_encoding
710 == _FEATURESET_MESSAGE_ENCODING_DELIMITED
711 and self.message_type
712 and not self.message_type.GetOptions().map_entry
713 and not self.containing_type.GetOptions().map_entry
714 ):
715 return FieldDescriptor.TYPE_GROUP
716 return self._type
717
718 @type.setter
719 def type(self, val):
720 self._type = val
721
722 @property
723 def label(self):
724 if (
725 self._GetFeatures().field_presence
726 == _FEATURESET_FIELD_PRESENCE_LEGACY_REQUIRED
727 ):
728 return FieldDescriptor.LABEL_REQUIRED
729 return self._label
730
731 @property
732 def is_required(self):
733 """Returns if the field is required."""
734 return (
735 self._GetFeatures().field_presence
736 == _FEATURESET_FIELD_PRESENCE_LEGACY_REQUIRED
737 )
738
739 @property
740 def is_repeated(self):
741 """Returns if the field is repeated."""
742 return self._label == FieldDescriptor.LABEL_REPEATED
743
744 @property
745 def camelcase_name(self):
746 """Camelcase name of this field.
747
748 Returns:
749 str: the name in CamelCase.
750 """
751 if self._camelcase_name is None:
752 self._camelcase_name = _ToCamelCase(self.name)
753 return self._camelcase_name
754
755 @property
756 def has_presence(self):
757 """Whether the field distinguishes between unpopulated and default values.
758
759 Raises:
760 RuntimeError: singular field that is not linked with message nor file.
761 """
762 if self.is_repeated:
763 return False
764 if (
765 self.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE
766 or self.is_extension
767 or self.containing_oneof
768 ):
769 return True
770
771 return (
772 self._GetFeatures().field_presence
773 != _FEATURESET_FIELD_PRESENCE_IMPLICIT
774 )
775
776 @property
777 def is_packed(self):
778 """Returns if the field is packed."""
779 if not self.is_repeated:
780 return False
781 field_type = self.type
782 if (field_type == FieldDescriptor.TYPE_STRING or
783 field_type == FieldDescriptor.TYPE_GROUP or
784 field_type == FieldDescriptor.TYPE_MESSAGE or
785 field_type == FieldDescriptor.TYPE_BYTES):
786 return False
787
788 return (
789 self._GetFeatures().repeated_field_encoding
790 == _FEATURESET_REPEATED_FIELD_ENCODING_PACKED
791 )
792
793 @staticmethod
794 def ProtoTypeToCppProtoType(proto_type):
795 """Converts from a Python proto type to a C++ Proto Type.
796
797 The Python ProtocolBuffer classes specify both the 'Python' datatype and the
798 'C++' datatype - and they're not the same. This helper method should
799 translate from one to another.
800
801 Args:
802 proto_type: the Python proto type (descriptor.FieldDescriptor.TYPE_*)
803 Returns:
804 int: descriptor.FieldDescriptor.CPPTYPE_*, the C++ type.
805 Raises:
806 TypeTransformationError: when the Python proto type isn't known.
807 """
808 try:
809 return FieldDescriptor._PYTHON_TO_CPP_PROTO_TYPE_MAP[proto_type]
810 except KeyError:
811 raise TypeTransformationError('Unknown proto_type: %s' % proto_type)
812
813
814class EnumDescriptor(_NestedDescriptorBase):
815
816 """Descriptor for an enum defined in a .proto file.
817
818 Attributes:
819 name (str): Name of the enum type.
820 full_name (str): Full name of the type, including package name
821 and any enclosing type(s).
822
823 values (list[EnumValueDescriptor]): List of the values
824 in this enum.
825 values_by_name (dict(str, EnumValueDescriptor)): Same as :attr:`values`,
826 but indexed by the "name" field of each EnumValueDescriptor.
827 values_by_number (dict(int, EnumValueDescriptor)): Same as :attr:`values`,
828 but indexed by the "number" field of each EnumValueDescriptor.
829 containing_type (Descriptor): Descriptor of the immediate containing
830 type of this enum, or None if this is an enum defined at the
831 top level in a .proto file. Set by Descriptor's constructor
832 if we're passed into one.
833 file (FileDescriptor): Reference to file descriptor.
834 options (descriptor_pb2.EnumOptions): Enum options message or
835 None to use default enum options.
836 """
837
838 if _USE_C_DESCRIPTORS:
839 _C_DESCRIPTOR_CLASS = _message.EnumDescriptor
840
841 def __new__(cls, name, full_name, filename, values,
842 containing_type=None, options=None,
843 serialized_options=None, file=None, # pylint: disable=redefined-builtin
844 serialized_start=None, serialized_end=None, create_key=None):
845 _message.Message._CheckCalledFromGeneratedFile()
846 return _message.default_pool.FindEnumTypeByName(full_name)
847
848 def __init__(self, name, full_name, filename, values,
849 containing_type=None, options=None,
850 serialized_options=None, file=None, # pylint: disable=redefined-builtin
851 serialized_start=None, serialized_end=None, create_key=None):
852 """Arguments are as described in the attribute description above.
853
854 Note that filename is an obsolete argument, that is not used anymore.
855 Please use file.name to access this as an attribute.
856 """
857 if create_key is not _internal_create_key:
858 _Deprecated('EnumDescriptor')
859
860 super(EnumDescriptor, self).__init__(
861 options, 'EnumOptions', name, full_name, file,
862 containing_type, serialized_start=serialized_start,
863 serialized_end=serialized_end, serialized_options=serialized_options)
864
865 self.values = values
866 for value in self.values:
867 value.file = file
868 value.type = self
869 self.values_by_name = dict((v.name, v) for v in values)
870 # Values are reversed to ensure that the first alias is retained.
871 self.values_by_number = dict((v.number, v) for v in reversed(values))
872
873 @property
874 def _parent(self):
875 return self.containing_type or self.file
876
877 @property
878 def is_closed(self):
879 """Returns true whether this is a "closed" enum.
880
881 This means that it:
882 - Has a fixed set of values, rather than being equivalent to an int32.
883 - Encountering values not in this set causes them to be treated as unknown
884 fields.
885 - The first value (i.e., the default) may be nonzero.
886
887 WARNING: Some runtimes currently have a quirk where non-closed enums are
888 treated as closed when used as the type of fields defined in a
889 `syntax = proto2;` file. This quirk is not present in all runtimes; as of
890 writing, we know that:
891
892 - C++, Java, and C++-based Python share this quirk.
893 - UPB and UPB-based Python do not.
894 - PHP and Ruby treat all enums as open regardless of declaration.
895
896 Care should be taken when using this function to respect the target
897 runtime's enum handling quirks.
898 """
899 return self._GetFeatures().enum_type == _FEATURESET_ENUM_TYPE_CLOSED
900
901 def CopyToProto(self, proto):
902 """Copies this to a descriptor_pb2.EnumDescriptorProto.
903
904 Args:
905 proto (descriptor_pb2.EnumDescriptorProto): An empty descriptor proto.
906 """
907 # This function is overridden to give a better doc comment.
908 super(EnumDescriptor, self).CopyToProto(proto)
909
910
911class EnumValueDescriptor(DescriptorBase):
912
913 """Descriptor for a single value within an enum.
914
915 Attributes:
916 name (str): Name of this value.
917 index (int): Dense, 0-indexed index giving the order that this
918 value appears textually within its enum in the .proto file.
919 number (int): Actual number assigned to this enum value.
920 type (EnumDescriptor): :class:`EnumDescriptor` to which this value
921 belongs. Set by :class:`EnumDescriptor`'s constructor if we're
922 passed into one.
923 options (descriptor_pb2.EnumValueOptions): Enum value options message or
924 None to use default enum value options options.
925 """
926
927 if _USE_C_DESCRIPTORS:
928 _C_DESCRIPTOR_CLASS = _message.EnumValueDescriptor
929
930 def __new__(cls, name, index, number,
931 type=None, # pylint: disable=redefined-builtin
932 options=None, serialized_options=None, create_key=None):
933 _message.Message._CheckCalledFromGeneratedFile()
934 # There is no way we can build a complete EnumValueDescriptor with the
935 # given parameters (the name of the Enum is not known, for example).
936 # Fortunately generated files just pass it to the EnumDescriptor()
937 # constructor, which will ignore it, so returning None is good enough.
938 return None
939
940 def __init__(self, name, index, number,
941 type=None, # pylint: disable=redefined-builtin
942 options=None, serialized_options=None, create_key=None):
943 """Arguments are as described in the attribute description above."""
944 if create_key is not _internal_create_key:
945 _Deprecated('EnumValueDescriptor')
946
947 super(EnumValueDescriptor, self).__init__(
948 type.file if type else None,
949 options,
950 serialized_options,
951 'EnumValueOptions',
952 )
953 self.name = name
954 self.index = index
955 self.number = number
956 self.type = type
957
958 @property
959 def _parent(self):
960 return self.type
961
962
963class OneofDescriptor(DescriptorBase):
964 """Descriptor for a oneof field.
965
966 Attributes:
967 name (str): Name of the oneof field.
968 full_name (str): Full name of the oneof field, including package name.
969 index (int): 0-based index giving the order of the oneof field inside
970 its containing type.
971 containing_type (Descriptor): :class:`Descriptor` of the protocol message
972 type that contains this field. Set by the :class:`Descriptor` constructor
973 if we're passed into one.
974 fields (list[FieldDescriptor]): The list of field descriptors this
975 oneof can contain.
976 """
977
978 if _USE_C_DESCRIPTORS:
979 _C_DESCRIPTOR_CLASS = _message.OneofDescriptor
980
981 def __new__(
982 cls, name, full_name, index, containing_type, fields, options=None,
983 serialized_options=None, create_key=None):
984 _message.Message._CheckCalledFromGeneratedFile()
985 return _message.default_pool.FindOneofByName(full_name)
986
987 def __init__(
988 self, name, full_name, index, containing_type, fields, options=None,
989 serialized_options=None, create_key=None):
990 """Arguments are as described in the attribute description above."""
991 if create_key is not _internal_create_key:
992 _Deprecated('OneofDescriptor')
993
994 super(OneofDescriptor, self).__init__(
995 containing_type.file if containing_type else None,
996 options,
997 serialized_options,
998 'OneofOptions',
999 )
1000 self.name = name
1001 self.full_name = full_name
1002 self.index = index
1003 self.containing_type = containing_type
1004 self.fields = fields
1005
1006 @property
1007 def _parent(self):
1008 return self.containing_type
1009
1010
1011class ServiceDescriptor(_NestedDescriptorBase):
1012
1013 """Descriptor for a service.
1014
1015 Attributes:
1016 name (str): Name of the service.
1017 full_name (str): Full name of the service, including package name.
1018 index (int): 0-indexed index giving the order that this services
1019 definition appears within the .proto file.
1020 methods (list[MethodDescriptor]): List of methods provided by this
1021 service.
1022 methods_by_name (dict(str, MethodDescriptor)): Same
1023 :class:`MethodDescriptor` objects as in :attr:`methods_by_name`, but
1024 indexed by "name" attribute in each :class:`MethodDescriptor`.
1025 options (descriptor_pb2.ServiceOptions): Service options message or
1026 None to use default service options.
1027 file (FileDescriptor): Reference to file info.
1028 """
1029
1030 if _USE_C_DESCRIPTORS:
1031 _C_DESCRIPTOR_CLASS = _message.ServiceDescriptor
1032
1033 def __new__(
1034 cls,
1035 name=None,
1036 full_name=None,
1037 index=None,
1038 methods=None,
1039 options=None,
1040 serialized_options=None,
1041 file=None, # pylint: disable=redefined-builtin
1042 serialized_start=None,
1043 serialized_end=None,
1044 create_key=None):
1045 _message.Message._CheckCalledFromGeneratedFile() # pylint: disable=protected-access
1046 return _message.default_pool.FindServiceByName(full_name)
1047
1048 def __init__(self, name, full_name, index, methods, options=None,
1049 serialized_options=None, file=None, # pylint: disable=redefined-builtin
1050 serialized_start=None, serialized_end=None, create_key=None):
1051 if create_key is not _internal_create_key:
1052 _Deprecated('ServiceDescriptor')
1053
1054 super(ServiceDescriptor, self).__init__(
1055 options, 'ServiceOptions', name, full_name, file,
1056 None, serialized_start=serialized_start,
1057 serialized_end=serialized_end, serialized_options=serialized_options)
1058 self.index = index
1059 self.methods = methods
1060 self.methods_by_name = dict((m.name, m) for m in methods)
1061 # Set the containing service for each method in this service.
1062 for method in self.methods:
1063 method.file = self.file
1064 method.containing_service = self
1065
1066 @property
1067 def _parent(self):
1068 return self.file
1069
1070 def FindMethodByName(self, name):
1071 """Searches for the specified method, and returns its descriptor.
1072
1073 Args:
1074 name (str): Name of the method.
1075
1076 Returns:
1077 MethodDescriptor: The descriptor for the requested method.
1078
1079 Raises:
1080 KeyError: if the method cannot be found in the service.
1081 """
1082 return self.methods_by_name[name]
1083
1084 def CopyToProto(self, proto):
1085 """Copies this to a descriptor_pb2.ServiceDescriptorProto.
1086
1087 Args:
1088 proto (descriptor_pb2.ServiceDescriptorProto): An empty descriptor proto.
1089 """
1090 # This function is overridden to give a better doc comment.
1091 super(ServiceDescriptor, self).CopyToProto(proto)
1092
1093
1094class MethodDescriptor(DescriptorBase):
1095
1096 """Descriptor for a method in a service.
1097
1098 Attributes:
1099 name (str): Name of the method within the service.
1100 full_name (str): Full name of method.
1101 index (int): 0-indexed index of the method inside the service.
1102 containing_service (ServiceDescriptor): The service that contains this
1103 method.
1104 input_type (Descriptor): The descriptor of the message that this method
1105 accepts.
1106 output_type (Descriptor): The descriptor of the message that this method
1107 returns.
1108 client_streaming (bool): Whether this method uses client streaming.
1109 server_streaming (bool): Whether this method uses server streaming.
1110 options (descriptor_pb2.MethodOptions or None): Method options message, or
1111 None to use default method options.
1112 """
1113
1114 if _USE_C_DESCRIPTORS:
1115 _C_DESCRIPTOR_CLASS = _message.MethodDescriptor
1116
1117 def __new__(cls,
1118 name,
1119 full_name,
1120 index,
1121 containing_service,
1122 input_type,
1123 output_type,
1124 client_streaming=False,
1125 server_streaming=False,
1126 options=None,
1127 serialized_options=None,
1128 create_key=None):
1129 _message.Message._CheckCalledFromGeneratedFile() # pylint: disable=protected-access
1130 return _message.default_pool.FindMethodByName(full_name)
1131
1132 def __init__(self,
1133 name,
1134 full_name,
1135 index,
1136 containing_service,
1137 input_type,
1138 output_type,
1139 client_streaming=False,
1140 server_streaming=False,
1141 options=None,
1142 serialized_options=None,
1143 create_key=None):
1144 """The arguments are as described in the description of MethodDescriptor
1145 attributes above.
1146
1147 Note that containing_service may be None, and may be set later if necessary.
1148 """
1149 if create_key is not _internal_create_key:
1150 _Deprecated('MethodDescriptor')
1151
1152 super(MethodDescriptor, self).__init__(
1153 containing_service.file if containing_service else None,
1154 options,
1155 serialized_options,
1156 'MethodOptions',
1157 )
1158 self.name = name
1159 self.full_name = full_name
1160 self.index = index
1161 self.containing_service = containing_service
1162 self.input_type = input_type
1163 self.output_type = output_type
1164 self.client_streaming = client_streaming
1165 self.server_streaming = server_streaming
1166
1167 @property
1168 def _parent(self):
1169 return self.containing_service
1170
1171 def CopyToProto(self, proto):
1172 """Copies this to a descriptor_pb2.MethodDescriptorProto.
1173
1174 Args:
1175 proto (descriptor_pb2.MethodDescriptorProto): An empty descriptor proto.
1176
1177 Raises:
1178 Error: If self couldn't be serialized, due to too few constructor
1179 arguments.
1180 """
1181 if self.containing_service is not None:
1182 from google.protobuf import descriptor_pb2
1183 service_proto = descriptor_pb2.ServiceDescriptorProto()
1184 self.containing_service.CopyToProto(service_proto)
1185 proto.CopyFrom(service_proto.method[self.index])
1186 else:
1187 raise Error('Descriptor does not contain a service.')
1188
1189
1190class FileDescriptor(DescriptorBase):
1191 """Descriptor for a file. Mimics the descriptor_pb2.FileDescriptorProto.
1192
1193 Note that :attr:`enum_types_by_name`, :attr:`extensions_by_name`, and
1194 :attr:`dependencies` fields are only set by the
1195 :py:mod:`google.protobuf.message_factory` module, and not by the generated
1196 proto code.
1197
1198 Attributes:
1199 name (str): Name of file, relative to root of source tree.
1200 package (str): Name of the package
1201 edition (Edition): Enum value indicating edition of the file
1202 serialized_pb (bytes): Byte string of serialized
1203 :class:`descriptor_pb2.FileDescriptorProto`.
1204 dependencies (list[FileDescriptor]): List of other :class:`FileDescriptor`
1205 objects this :class:`FileDescriptor` depends on.
1206 public_dependencies (list[FileDescriptor]): A subset of
1207 :attr:`dependencies`, which were declared as "public".
1208 message_types_by_name (dict(str, Descriptor)): Mapping from message names to
1209 their :class:`Descriptor`.
1210 enum_types_by_name (dict(str, EnumDescriptor)): Mapping from enum names to
1211 their :class:`EnumDescriptor`.
1212 extensions_by_name (dict(str, FieldDescriptor)): Mapping from extension
1213 names declared at file scope to their :class:`FieldDescriptor`.
1214 services_by_name (dict(str, ServiceDescriptor)): Mapping from services'
1215 names to their :class:`ServiceDescriptor`.
1216 pool (DescriptorPool): The pool this descriptor belongs to. When not passed
1217 to the constructor, the global default pool is used.
1218 """
1219
1220 if _USE_C_DESCRIPTORS:
1221 _C_DESCRIPTOR_CLASS = _message.FileDescriptor
1222
1223 def __new__(
1224 cls,
1225 name,
1226 package,
1227 options=None,
1228 serialized_options=None,
1229 serialized_pb=None,
1230 dependencies=None,
1231 public_dependencies=None,
1232 syntax=None,
1233 edition=None,
1234 pool=None,
1235 create_key=None,
1236 ):
1237 # FileDescriptor() is called from various places, not only from generated
1238 # files, to register dynamic proto files and messages.
1239 # pylint: disable=g-explicit-bool-comparison
1240 if serialized_pb:
1241 return _message.default_pool.AddSerializedFile(serialized_pb)
1242 else:
1243 return super(FileDescriptor, cls).__new__(cls)
1244
1245 def __init__(
1246 self,
1247 name,
1248 package,
1249 options=None,
1250 serialized_options=None,
1251 serialized_pb=None,
1252 dependencies=None,
1253 public_dependencies=None,
1254 syntax=None,
1255 edition=None,
1256 pool=None,
1257 create_key=None,
1258 ):
1259 """Constructor."""
1260 if create_key is not _internal_create_key:
1261 _Deprecated('FileDescriptor')
1262
1263 super(FileDescriptor, self).__init__(
1264 self, options, serialized_options, 'FileOptions'
1265 )
1266
1267 if edition and edition != 'EDITION_UNKNOWN':
1268 self._edition = edition
1269 elif syntax == 'proto3':
1270 self._edition = 'EDITION_PROTO3'
1271 else:
1272 self._edition = 'EDITION_PROTO2'
1273
1274 if pool is None:
1275 from google.protobuf import descriptor_pool
1276 pool = descriptor_pool.Default()
1277 self.pool = pool
1278 self.message_types_by_name = {}
1279 self.name = name
1280 self.package = package
1281 self.serialized_pb = serialized_pb
1282
1283 self.enum_types_by_name = {}
1284 self.extensions_by_name = {}
1285 self.services_by_name = {}
1286 self.dependencies = (dependencies or [])
1287 self.public_dependencies = (public_dependencies or [])
1288
1289 def CopyToProto(self, proto):
1290 """Copies this to a descriptor_pb2.FileDescriptorProto.
1291
1292 Args:
1293 proto: An empty descriptor_pb2.FileDescriptorProto.
1294 """
1295 proto.ParseFromString(self.serialized_pb)
1296
1297 @property
1298 def _parent(self):
1299 return None
1300
1301
1302def _ParseOptions(message, string):
1303 """Parses serialized options.
1304
1305 This helper function is used to parse serialized options in generated
1306 proto2 files. It must not be used outside proto2.
1307 """
1308 message.ParseFromString(string)
1309 return message
1310
1311
1312def _ToCamelCase(name):
1313 """Converts name to camel-case and returns it."""
1314 capitalize_next = False
1315 result = []
1316
1317 for c in name:
1318 if c == '_':
1319 if result:
1320 capitalize_next = True
1321 elif capitalize_next:
1322 result.append(c.upper())
1323 capitalize_next = False
1324 else:
1325 result += c
1326
1327 # Lower-case the first letter.
1328 if result and result[0].isupper():
1329 result[0] = result[0].lower()
1330 return ''.join(result)
1331
1332
1333def _OptionsOrNone(descriptor_proto):
1334 """Returns the value of the field `options`, or None if it is not set."""
1335 if descriptor_proto.HasField('options'):
1336 return descriptor_proto.options
1337 else:
1338 return None
1339
1340
1341def _ToJsonName(name):
1342 """Converts name to Json name and returns it."""
1343 capitalize_next = False
1344 result = []
1345
1346 for c in name:
1347 if c == '_':
1348 capitalize_next = True
1349 elif capitalize_next:
1350 result.append(c.upper())
1351 capitalize_next = False
1352 else:
1353 result += c
1354
1355 return ''.join(result)
1356
1357
1358def MakeDescriptor(
1359 desc_proto,
1360 package='',
1361 build_file_if_cpp=True,
1362 syntax=None,
1363 edition=None,
1364 file_desc=None,
1365):
1366 """Make a protobuf Descriptor given a DescriptorProto protobuf.
1367
1368 Handles nested descriptors. Note that this is limited to the scope of defining
1369 a message inside of another message. Composite fields can currently only be
1370 resolved if the message is defined in the same scope as the field.
1371
1372 Args:
1373 desc_proto: The descriptor_pb2.DescriptorProto protobuf message.
1374 package: Optional package name for the new message Descriptor (string).
1375 build_file_if_cpp: Update the C++ descriptor pool if api matches. Set to
1376 False on recursion, so no duplicates are created.
1377 syntax: The syntax/semantics that should be used. Set to "proto3" to get
1378 proto3 field presence semantics.
1379 edition: The edition that should be used if syntax is "edition".
1380 file_desc: A FileDescriptor to place this descriptor into.
1381
1382 Returns:
1383 A Descriptor for protobuf messages.
1384 """
1385 # pylint: disable=g-import-not-at-top
1386 from google.protobuf import descriptor_pb2
1387
1388 # Generate a random name for this proto file to prevent conflicts with any
1389 # imported ones. We need to specify a file name so the descriptor pool
1390 # accepts our FileDescriptorProto, but it is not important what that file
1391 # name is actually set to.
1392 proto_name = binascii.hexlify(os.urandom(16)).decode('ascii')
1393
1394 if package:
1395 file_name = os.path.join(package.replace('.', '/'), proto_name + '.proto')
1396 else:
1397 file_name = proto_name + '.proto'
1398
1399 if api_implementation.Type() != 'python' and build_file_if_cpp:
1400 # The C++ implementation requires all descriptors to be backed by the same
1401 # definition in the C++ descriptor pool. To do this, we build a
1402 # FileDescriptorProto with the same definition as this descriptor and build
1403 # it into the pool.
1404 file_descriptor_proto = descriptor_pb2.FileDescriptorProto()
1405 file_descriptor_proto.message_type.add().MergeFrom(desc_proto)
1406
1407 if package:
1408 file_descriptor_proto.package = package
1409 file_descriptor_proto.name = file_name
1410
1411 _message.default_pool.Add(file_descriptor_proto)
1412 result = _message.default_pool.FindFileByName(file_descriptor_proto.name)
1413
1414 if _USE_C_DESCRIPTORS:
1415 return result.message_types_by_name[desc_proto.name]
1416
1417 if file_desc is None:
1418 file_desc = FileDescriptor(
1419 pool=None,
1420 name=file_name,
1421 package=package,
1422 syntax=syntax,
1423 edition=edition,
1424 options=None,
1425 serialized_pb='',
1426 dependencies=[],
1427 public_dependencies=[],
1428 create_key=_internal_create_key,
1429 )
1430 full_message_name = [desc_proto.name]
1431 if package: full_message_name.insert(0, package)
1432
1433 # Create Descriptors for enum types
1434 enum_types = {}
1435 for enum_proto in desc_proto.enum_type:
1436 full_name = '.'.join(full_message_name + [enum_proto.name])
1437 enum_desc = EnumDescriptor(
1438 enum_proto.name,
1439 full_name,
1440 None,
1441 [
1442 EnumValueDescriptor(
1443 enum_val.name,
1444 ii,
1445 enum_val.number,
1446 create_key=_internal_create_key,
1447 )
1448 for ii, enum_val in enumerate(enum_proto.value)
1449 ],
1450 file=file_desc,
1451 create_key=_internal_create_key,
1452 )
1453 enum_types[full_name] = enum_desc
1454
1455 # Create Descriptors for nested types
1456 nested_types = {}
1457 for nested_proto in desc_proto.nested_type:
1458 full_name = '.'.join(full_message_name + [nested_proto.name])
1459 # Nested types are just those defined inside of the message, not all types
1460 # used by fields in the message, so no loops are possible here.
1461 nested_desc = MakeDescriptor(
1462 nested_proto,
1463 package='.'.join(full_message_name),
1464 build_file_if_cpp=False,
1465 syntax=syntax,
1466 edition=edition,
1467 file_desc=file_desc,
1468 )
1469 nested_types[full_name] = nested_desc
1470
1471 fields = []
1472 for field_proto in desc_proto.field:
1473 full_name = '.'.join(full_message_name + [field_proto.name])
1474 enum_desc = None
1475 nested_desc = None
1476 if field_proto.json_name:
1477 json_name = field_proto.json_name
1478 else:
1479 json_name = None
1480 if field_proto.HasField('type_name'):
1481 type_name = field_proto.type_name
1482 full_type_name = '.'.join(full_message_name +
1483 [type_name[type_name.rfind('.')+1:]])
1484 if full_type_name in nested_types:
1485 nested_desc = nested_types[full_type_name]
1486 elif full_type_name in enum_types:
1487 enum_desc = enum_types[full_type_name]
1488 # Else type_name references a non-local type, which isn't implemented
1489 field = FieldDescriptor(
1490 field_proto.name,
1491 full_name,
1492 field_proto.number - 1,
1493 field_proto.number,
1494 field_proto.type,
1495 FieldDescriptor.ProtoTypeToCppProtoType(field_proto.type),
1496 field_proto.label,
1497 None,
1498 nested_desc,
1499 enum_desc,
1500 None,
1501 False,
1502 None,
1503 options=_OptionsOrNone(field_proto),
1504 has_default_value=False,
1505 json_name=json_name,
1506 file=file_desc,
1507 create_key=_internal_create_key,
1508 )
1509 fields.append(field)
1510
1511 desc_name = '.'.join(full_message_name)
1512 return Descriptor(
1513 desc_proto.name,
1514 desc_name,
1515 None,
1516 None,
1517 fields,
1518 list(nested_types.values()),
1519 list(enum_types.values()),
1520 [],
1521 options=_OptionsOrNone(desc_proto),
1522 file=file_desc,
1523 create_key=_internal_create_key,
1524 )