descriptor.py 52 KB

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