containers.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. """Contains container classes to represent different protocol buffer types.
  8. This file defines container classes which represent categories of protocol
  9. buffer field types which need extra maintenance. Currently these categories
  10. are:
  11. - Repeated scalar fields - These are all repeated fields which aren't
  12. composite (e.g. they are of simple types like int32, string, etc).
  13. - Repeated composite fields - Repeated fields which are composite. This
  14. includes groups and nested messages.
  15. """
  16. import collections.abc
  17. import copy
  18. import pickle
  19. import warnings
  20. from typing import (
  21. Any,
  22. Iterable,
  23. Iterator,
  24. List,
  25. MutableMapping,
  26. MutableSequence,
  27. NoReturn,
  28. Optional,
  29. Sequence,
  30. TypeVar,
  31. Union,
  32. overload,
  33. )
  34. _T = TypeVar('_T')
  35. _K = TypeVar('_K')
  36. _V = TypeVar('_V')
  37. from google.protobuf.descriptor import FieldDescriptor
  38. from google.protobuf import message
  39. def _CheckFrozen(is_frozen: bool, msg: str) -> None:
  40. if is_frozen:
  41. warnings.warn(
  42. 'Mutating messages or containers returned by GetOptions() is'
  43. ' deprecated and will raise an exception in a future release.',
  44. category=FutureWarning,
  45. stacklevel=3,
  46. )
  47. class BaseContainer(Sequence[_T]):
  48. """Base container class."""
  49. # Minimizes memory usage and disallows assignment to other attributes.
  50. __slots__ = ['_message_listener', '_values', '_frozen']
  51. def __init__(self, message_listener: Any) -> None:
  52. """Args:
  53. message_listener: A MessageListener implementation.
  54. The RepeatedScalarFieldContainer will call this object's
  55. Modified() method when it is modified.
  56. """
  57. self._message_listener = message_listener
  58. self._values = []
  59. self._frozen = False
  60. @overload
  61. def __getitem__(self, key: int) -> _T:
  62. ...
  63. @overload
  64. def __getitem__(self, key: slice) -> List[_T]:
  65. ...
  66. def __getitem__(self, key):
  67. """Retrieves item by the specified key."""
  68. return self._values[key]
  69. def __len__(self) -> int:
  70. """Returns the number of elements in the container."""
  71. return len(self._values)
  72. def __ne__(self, other: Any) -> bool:
  73. """Checks if another instance isn't equal to this one."""
  74. # The concrete classes should define __eq__.
  75. return not self == other
  76. __hash__ = None
  77. def __repr__(self) -> str:
  78. return repr(self._values)
  79. def _SetFrozen(self) -> None:
  80. self._frozen = True
  81. def _AssureWritable(self) -> 'BaseContainer[_T]':
  82. _CheckFrozen(self._frozen, 'Container is immutable')
  83. return self
  84. def sort(self, *args, **kwargs) -> None:
  85. self._AssureWritable()
  86. # Continue to support the old sort_function keyword argument.
  87. # This is expected to be a rare occurrence, so use LBYL to avoid
  88. # the overhead of actually catching KeyError.
  89. if 'sort_function' in kwargs:
  90. kwargs['cmp'] = kwargs.pop('sort_function')
  91. self._values.sort(*args, **kwargs)
  92. def reverse(self) -> None:
  93. self._AssureWritable()
  94. self._values.reverse()
  95. # TODO: Remove this. BaseContainer does *not* conform to
  96. # MutableSequence, only its subclasses do.
  97. collections.abc.MutableSequence.register(BaseContainer)
  98. class RepeatedScalarFieldContainer(BaseContainer[_T], MutableSequence[_T]):
  99. """Simple, type-checked, list-like container for holding repeated scalars."""
  100. # Disallows assignment to other attributes.
  101. __slots__ = ['_type_checker', '_field']
  102. def __init__(
  103. self,
  104. message_listener: Any,
  105. type_checker: Any,
  106. field: Any = None,
  107. ) -> None:
  108. """Args:
  109. message_listener: A MessageListener implementation. The
  110. RepeatedScalarFieldContainer will call this object's Modified() method
  111. when it is modified.
  112. type_checker: A type_checkers.ValueChecker instance to run on elements
  113. inserted into this container.
  114. """
  115. super().__init__(message_listener)
  116. self._type_checker = type_checker
  117. self._field = field
  118. def append(self, value: _T) -> None:
  119. """Appends an item to the list. Similar to list.append()."""
  120. self._AssureWritable()
  121. self._values.append(self._type_checker.CheckValue(value))
  122. if not self._message_listener.dirty:
  123. self._message_listener.Modified()
  124. def insert(self, key: int, value: _T) -> None:
  125. """Inserts the item at the specified position. Similar to list.insert()."""
  126. self._AssureWritable()
  127. self._values.insert(key, self._type_checker.CheckValue(value))
  128. if not self._message_listener.dirty:
  129. self._message_listener.Modified()
  130. def extend(self, elem_seq: Iterable[_T]) -> None:
  131. """Extends by appending the given iterable. Similar to list.extend()."""
  132. self._AssureWritable()
  133. elem_seq_iter = iter(elem_seq)
  134. new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  135. if new_values:
  136. self._values.extend(new_values)
  137. self._message_listener.Modified()
  138. def MergeFrom(
  139. self,
  140. other: Union['RepeatedScalarFieldContainer[_T]', Iterable[_T]],
  141. ) -> None:
  142. """Appends the contents of another repeated field of the same type to this
  143. one. We do not check the types of the individual fields.
  144. """
  145. self._AssureWritable()
  146. self._values.extend(other)
  147. self._message_listener.Modified()
  148. def remove(self, elem: _T):
  149. """Removes an item from the list. Similar to list.remove()."""
  150. self._AssureWritable()
  151. self._values.remove(elem)
  152. self._message_listener.Modified()
  153. def pop(self, key: Optional[int] = -1) -> _T:
  154. """Removes and returns an item at a given index. Similar to list.pop()."""
  155. self._AssureWritable()
  156. value = self._values[key]
  157. self.__delitem__(key)
  158. return value
  159. @overload
  160. def __setitem__(self, key: int, value: _T) -> None:
  161. ...
  162. @overload
  163. def __setitem__(self, key: slice, value: Iterable[_T]) -> None:
  164. ...
  165. def __setitem__(self, key, value) -> None:
  166. """Sets the item on the specified position."""
  167. self._AssureWritable()
  168. if isinstance(key, slice):
  169. if key.step is not None:
  170. raise ValueError('Extended slices not supported')
  171. self._values[key] = map(self._type_checker.CheckValue, value)
  172. self._message_listener.Modified()
  173. else:
  174. self._values[key] = self._type_checker.CheckValue(value)
  175. self._message_listener.Modified()
  176. def __delitem__(self, key: Union[int, slice]) -> None:
  177. """Deletes the item at the specified position."""
  178. self._AssureWritable()
  179. del self._values[key]
  180. self._message_listener.Modified()
  181. def __eq__(self, other: Any) -> bool:
  182. """Compares the current instance with another one."""
  183. if self is other:
  184. return True
  185. # Special case for the same type which should be common and fast.
  186. if isinstance(other, self.__class__):
  187. return other._values == self._values
  188. # We are presumably comparing against some other sequence type.
  189. return other == self._values
  190. def __deepcopy__(
  191. self,
  192. unused_memo: Any = None,
  193. ) -> 'RepeatedScalarFieldContainer[_T]':
  194. clone = RepeatedScalarFieldContainer(
  195. copy.deepcopy(self._message_listener), self._type_checker, self._field
  196. )
  197. clone.MergeFrom(self)
  198. return clone
  199. def __reduce__(self, **kwargs) -> NoReturn:
  200. raise pickle.PickleError(
  201. "Can't pickle repeated scalar fields, convert to list first"
  202. )
  203. def __array__(self, dtype=None, copy=None):
  204. import numpy as np
  205. if dtype is None:
  206. cpp_type = self._field.cpp_type
  207. if cpp_type == FieldDescriptor.CPPTYPE_INT32:
  208. dtype = np.int32
  209. elif cpp_type == FieldDescriptor.CPPTYPE_INT64:
  210. dtype = np.int64
  211. elif cpp_type == FieldDescriptor.CPPTYPE_UINT32:
  212. dtype = np.uint32
  213. elif cpp_type == FieldDescriptor.CPPTYPE_UINT64:
  214. dtype = np.uint64
  215. elif cpp_type == FieldDescriptor.CPPTYPE_DOUBLE:
  216. dtype = np.float64
  217. elif cpp_type == FieldDescriptor.CPPTYPE_FLOAT:
  218. dtype = np.float32
  219. elif cpp_type == FieldDescriptor.CPPTYPE_BOOL:
  220. dtype = np.bool
  221. elif cpp_type == FieldDescriptor.CPPTYPE_ENUM:
  222. dtype = np.int32
  223. elif self._field.type == FieldDescriptor.TYPE_BYTES:
  224. dtype = 'S'
  225. elif self._field.type == FieldDescriptor.TYPE_STRING:
  226. dtype = str
  227. else:
  228. raise SystemError(
  229. 'Code should never reach here: message type detected in'
  230. ' RepeatedScalarFieldContainer'
  231. )
  232. return np.array(self._values, dtype=dtype, copy=True)
  233. # TODO: Constrain T to be a subtype of Message.
  234. class RepeatedCompositeFieldContainer(BaseContainer[_T], MutableSequence[_T]):
  235. """Simple, list-like container for holding repeated composite fields."""
  236. # Disallows assignment to other attributes.
  237. __slots__ = ['_message_descriptor']
  238. def __init__(self, message_listener: Any, message_descriptor: Any) -> None:
  239. """Note that we pass in a descriptor instead of the generated directly,
  240. since at the time we construct a _RepeatedCompositeFieldContainer we
  241. haven't yet necessarily initialized the type that will be contained in the
  242. container.
  243. Args:
  244. message_listener: A MessageListener implementation. The
  245. RepeatedCompositeFieldContainer will call this object's Modified()
  246. method when it is modified.
  247. message_descriptor: A Descriptor instance describing the protocol type
  248. that should be present in this container. We'll use the _concrete_class
  249. field of this descriptor when the client calls add().
  250. """
  251. super().__init__(message_listener)
  252. self._message_descriptor = message_descriptor
  253. def _SetFrozen(self) -> None:
  254. super()._SetFrozen()
  255. for val in self._values:
  256. val._SetFrozen()
  257. def add(self, **kwargs: Any) -> _T:
  258. """Adds a new element at the end of the list and returns it.
  259. Keyword arguments may be used to initialize the element.
  260. """
  261. self._AssureWritable()
  262. new_element = self._message_descriptor._concrete_class(**kwargs)
  263. new_element._SetListener(self._message_listener)
  264. self._values.append(new_element)
  265. if not self._message_listener.dirty:
  266. self._message_listener.Modified()
  267. return new_element
  268. def append(self, value: _T) -> None:
  269. """Appends one element by copying the message."""
  270. self._AssureWritable()
  271. new_element = self._message_descriptor._concrete_class()
  272. new_element._SetListener(self._message_listener)
  273. new_element.CopyFrom(value)
  274. self._values.append(new_element)
  275. if not self._message_listener.dirty:
  276. self._message_listener.Modified()
  277. def insert(self, key: int, value: _T) -> None:
  278. """Inserts the item at the specified position by copying."""
  279. self._AssureWritable()
  280. new_element = self._message_descriptor._concrete_class()
  281. new_element._SetListener(self._message_listener)
  282. new_element.CopyFrom(value)
  283. self._values.insert(key, new_element)
  284. if not self._message_listener.dirty:
  285. self._message_listener.Modified()
  286. def extend(self, elem_seq: Iterable[_T]) -> None:
  287. """Extends by appending the given sequence of elements of the same type
  288. as this one, copying each individual message.
  289. """
  290. self._AssureWritable()
  291. message_class = self._message_descriptor._concrete_class
  292. listener = self._message_listener
  293. values = self._values
  294. for message in elem_seq:
  295. new_element = message_class()
  296. new_element._SetListener(listener)
  297. new_element.MergeFrom(message)
  298. values.append(new_element)
  299. listener.Modified()
  300. def MergeFrom(
  301. self,
  302. other: Union['RepeatedCompositeFieldContainer[_T]', Iterable[_T]],
  303. ) -> None:
  304. """Appends the contents of another repeated field of the same type to this
  305. one, copying each individual message.
  306. """
  307. self._AssureWritable()
  308. self.extend(other)
  309. def remove(self, elem: _T) -> None:
  310. """Removes an item from the list. Similar to list.remove()."""
  311. self._AssureWritable()
  312. self._values.remove(elem)
  313. self._message_listener.Modified()
  314. def pop(self, key: Optional[int] = -1) -> _T:
  315. """Removes and returns an item at a given index. Similar to list.pop()."""
  316. self._AssureWritable()
  317. value = self._values[key]
  318. self.__delitem__(key)
  319. return value
  320. @overload
  321. def __setitem__(self, key: int, value: _T) -> None:
  322. ...
  323. @overload
  324. def __setitem__(self, key: slice, value: Iterable[_T]) -> None:
  325. ...
  326. def __setitem__(self, key, value):
  327. # This method is implemented to make RepeatedCompositeFieldContainer
  328. # structurally compatible with typing.MutableSequence. It is
  329. # otherwise unsupported and will always raise an error.
  330. raise TypeError(
  331. f'{self.__class__.__name__} object does not support item assignment'
  332. )
  333. def __delitem__(self, key: Union[int, slice]) -> None:
  334. """Deletes the item at the specified position."""
  335. self._AssureWritable()
  336. del self._values[key]
  337. self._message_listener.Modified()
  338. def __eq__(self, other: Any) -> bool:
  339. """Compares the current instance with another one."""
  340. if self is other:
  341. return True
  342. if not isinstance(other, self.__class__):
  343. raise TypeError(
  344. 'Can only compare repeated composite fields against '
  345. 'other repeated composite fields.'
  346. )
  347. return self._values == other._values
  348. class ScalarMap(MutableMapping[_K, _V]):
  349. """Simple, type-checked, dict-like container for holding repeated scalars."""
  350. # Disallows assignment to other attributes.
  351. __slots__ = [
  352. '_key_checker',
  353. '_value_checker',
  354. '_values',
  355. '_message_listener',
  356. '_entry_descriptor',
  357. '_frozen',
  358. ]
  359. def __init__(
  360. self,
  361. message_listener: Any,
  362. key_checker: Any,
  363. value_checker: Any,
  364. entry_descriptor: Any,
  365. ) -> None:
  366. """Args:
  367. message_listener: A MessageListener implementation.
  368. The ScalarMap will call this object's Modified() method when it
  369. is modified.
  370. key_checker: A type_checkers.ValueChecker instance to run on keys
  371. inserted into this container.
  372. value_checker: A type_checkers.ValueChecker instance to run on values
  373. inserted into this container.
  374. entry_descriptor: The MessageDescriptor of a map entry: key and value.
  375. """
  376. self._message_listener = message_listener
  377. self._key_checker = key_checker
  378. self._value_checker = value_checker
  379. self._entry_descriptor = entry_descriptor
  380. self._values = {}
  381. self._frozen = False
  382. def _SetFrozen(self) -> None:
  383. self._frozen = True
  384. def _AssureWritable(self) -> 'ScalarMap[_K, _V]':
  385. _CheckFrozen(self._frozen, 'Map is immutable')
  386. return self
  387. def __getitem__(self, key: _K) -> _V:
  388. key = self._key_checker.CheckValue(key)
  389. try:
  390. return self._values[key]
  391. except KeyError:
  392. self._AssureWritable()
  393. val = self._value_checker.DefaultValue()
  394. self._values[key] = val
  395. return val
  396. def __contains__(self, item: _K) -> bool:
  397. # We check the key's type to match the strong-typing flavor of the API.
  398. # Also this makes it easier to match the behavior of the C++ implementation.
  399. item = self._key_checker.CheckValue(item)
  400. return item in self._values
  401. @overload
  402. def get(self, key: _K) -> Optional[_V]:
  403. ...
  404. @overload
  405. def get(self, key: _K, default: _T) -> Union[_V, _T]:
  406. ...
  407. # We need to override this explicitly, because our defaultdict-like behavior
  408. # will make the default implementation (from our base class) always insert
  409. # the key.
  410. def get(self, key, default=None):
  411. checked_key = self._key_checker.CheckValue(key)
  412. if checked_key in self._values:
  413. return self[checked_key]
  414. else:
  415. return default
  416. def __setitem__(self, key: _K, value: _V) -> _T:
  417. self._AssureWritable()
  418. checked_key = self._key_checker.CheckValue(key)
  419. checked_value = self._value_checker.CheckValue(value)
  420. self._values[checked_key] = checked_value
  421. self._message_listener.Modified()
  422. def __delitem__(self, key: _K) -> None:
  423. self._AssureWritable()
  424. checked_key = self._key_checker.CheckValue(key)
  425. del self._values[checked_key]
  426. self._message_listener.Modified()
  427. def __len__(self) -> int:
  428. return len(self._values)
  429. def __iter__(self) -> Iterator[_K]:
  430. return iter(self._values)
  431. def __repr__(self) -> str:
  432. return repr(self._values)
  433. def setdefault(self, key: _K, value: Optional[_V] = None) -> _V:
  434. self._AssureWritable()
  435. checked_key = self._key_checker.CheckValue(key)
  436. if value == None:
  437. raise ValueError('The value for scalar map setdefault must be set.')
  438. if checked_key not in self._values:
  439. self.__setitem__(checked_key, value)
  440. return self[key]
  441. def MergeFrom(self, other: 'ScalarMap[_K, _V]') -> None:
  442. self._AssureWritable()
  443. self._values.update(other._values)
  444. self._message_listener.Modified()
  445. def InvalidateIterators(self) -> None:
  446. # It appears that the only way to reliably invalidate iterators to
  447. # self._values is to ensure that its size changes.
  448. original = self._values
  449. self._values = original.copy()
  450. original[None] = None
  451. # This is defined in the abstract base, but we can do it much more cheaply.
  452. def clear(self) -> None:
  453. self._AssureWritable()
  454. self._values.clear()
  455. self._message_listener.Modified()
  456. def GetEntryClass(self) -> Any:
  457. return self._entry_descriptor._concrete_class
  458. class MessageMap(MutableMapping[_K, _V]):
  459. """Simple, type-checked, dict-like container for with submessage values."""
  460. # Disallows assignment to other attributes.
  461. __slots__ = [
  462. '_key_checker',
  463. '_values',
  464. '_message_listener',
  465. '_message_descriptor',
  466. '_entry_descriptor',
  467. '_frozen',
  468. ]
  469. def __init__(
  470. self,
  471. message_listener: Any,
  472. message_descriptor: Any,
  473. key_checker: Any,
  474. entry_descriptor: Any,
  475. ) -> None:
  476. """Args:
  477. message_listener: A MessageListener implementation.
  478. The ScalarMap will call this object's Modified() method when it
  479. is modified.
  480. key_checker: A type_checkers.ValueChecker instance to run on keys
  481. inserted into this container.
  482. value_checker: A type_checkers.ValueChecker instance to run on values
  483. inserted into this container.
  484. entry_descriptor: The MessageDescriptor of a map entry: key and value.
  485. """
  486. self._message_listener = message_listener
  487. self._message_descriptor = message_descriptor
  488. self._key_checker = key_checker
  489. self._entry_descriptor = entry_descriptor
  490. self._values = {}
  491. self._frozen = False
  492. def _SetFrozen(self) -> None:
  493. self._frozen = True
  494. for val in self._values.values():
  495. val._SetFrozen()
  496. def _AssureWritable(self) -> 'MessageMap[_K, _V]':
  497. _CheckFrozen(self._frozen, 'Map is immutable')
  498. return self
  499. def __getitem__(self, key: _K) -> _V:
  500. key = self._key_checker.CheckValue(key)
  501. try:
  502. return self._values[key]
  503. except KeyError:
  504. self._AssureWritable()
  505. new_element = self._message_descriptor._concrete_class()
  506. new_element._SetListener(self._message_listener)
  507. self._values[key] = new_element
  508. self._message_listener.Modified()
  509. return new_element
  510. def get_or_create(self, key: _K) -> _V:
  511. """get_or_create() is an alias for getitem (ie. map[key]).
  512. Args:
  513. key: The key to get or create in the map.
  514. This is useful in cases where you want to be explicit that the call is
  515. mutating the map. This can avoid lint errors for statements like this
  516. that otherwise would appear to be pointless statements:
  517. msg.my_map[key]
  518. """
  519. return self[key]
  520. @overload
  521. def get(self, key: _K) -> Optional[_V]:
  522. ...
  523. @overload
  524. def get(self, key: _K, default: _T) -> Union[_V, _T]:
  525. ...
  526. # We need to override this explicitly, because our defaultdict-like behavior
  527. # will make the default implementation (from our base class) always insert
  528. # the key.
  529. def get(self, key, default=None):
  530. if key in self:
  531. return self[key]
  532. else:
  533. return default
  534. def __contains__(self, item: _K) -> bool:
  535. item = self._key_checker.CheckValue(item)
  536. return item in self._values
  537. def __setitem__(self, key: _K, value: _V) -> NoReturn:
  538. self._AssureWritable()
  539. raise ValueError('May not set values directly, call my_map[key].foo = 5')
  540. def __delitem__(self, key: _K) -> None:
  541. self._AssureWritable()
  542. key = self._key_checker.CheckValue(key)
  543. del self._values[key]
  544. self._message_listener.Modified()
  545. def __len__(self) -> int:
  546. return len(self._values)
  547. def __iter__(self) -> Iterator[_K]:
  548. return iter(self._values)
  549. def __repr__(self) -> str:
  550. return repr(self._values)
  551. def setdefault(self, key: _K, value: Optional[_V] = None) -> _V:
  552. self._AssureWritable()
  553. raise NotImplementedError(
  554. 'Set message map value directly is not supported, call'
  555. ' my_map[key].foo = 5'
  556. )
  557. def MergeFrom(self, other: 'MessageMap[_K, _V]') -> None:
  558. self._AssureWritable()
  559. # pylint: disable=protected-access
  560. for key in other._values:
  561. # According to documentation: "When parsing from the wire or when merging,
  562. # if there are duplicate map keys the last key seen is used".
  563. if key in self:
  564. del self[key]
  565. self[key].CopyFrom(other[key])
  566. # self._message_listener.Modified() not required here, because
  567. # mutations to submessages already propagate.
  568. def InvalidateIterators(self) -> None:
  569. # It appears that the only way to reliably invalidate iterators to
  570. # self._values is to ensure that its size changes.
  571. original = self._values
  572. self._values = original.copy()
  573. original[None] = None
  574. # This is defined in the abstract base, but we can do it much more cheaply.
  575. def clear(self) -> None:
  576. self._AssureWritable()
  577. self._values.clear()
  578. self._message_listener.Modified()
  579. def GetEntryClass(self) -> Any:
  580. return self._entry_descriptor._concrete_class
  581. class _UnknownField:
  582. """A parsed unknown field."""
  583. # Disallows assignment to other attributes.
  584. __slots__ = ['_field_number', '_wire_type', '_data']
  585. def __init__(self, field_number, wire_type, data):
  586. self._field_number = field_number
  587. self._wire_type = wire_type
  588. self._data = data
  589. return
  590. def __lt__(self, other):
  591. # pylint: disable=protected-access
  592. return self._field_number < other._field_number
  593. def __eq__(self, other):
  594. if self is other:
  595. return True
  596. # pylint: disable=protected-access
  597. return (
  598. self._field_number == other._field_number
  599. and self._wire_type == other._wire_type
  600. and self._data == other._data
  601. )
  602. class UnknownFieldRef: # pylint: disable=missing-class-docstring
  603. def __init__(self, parent, index):
  604. self._parent = parent
  605. self._index = index
  606. def _check_valid(self):
  607. if not self._parent:
  608. raise ValueError(
  609. 'UnknownField does not exist. The parent message might be cleared.'
  610. )
  611. if self._index >= len(self._parent):
  612. raise ValueError(
  613. 'UnknownField does not exist. The parent message might be cleared.'
  614. )
  615. @property
  616. def field_number(self):
  617. self._check_valid()
  618. # pylint: disable=protected-access
  619. return self._parent._internal_get(self._index)._field_number
  620. @property
  621. def wire_type(self):
  622. self._check_valid()
  623. # pylint: disable=protected-access
  624. return self._parent._internal_get(self._index)._wire_type
  625. @property
  626. def data(self):
  627. self._check_valid()
  628. # pylint: disable=protected-access
  629. return self._parent._internal_get(self._index)._data
  630. class UnknownFieldSet:
  631. """UnknownField container"""
  632. # Disallows assignment to other attributes.
  633. __slots__ = ['_values']
  634. def __init__(self):
  635. self._values = []
  636. def __getitem__(self, index):
  637. if self._values is None:
  638. raise ValueError(
  639. 'UnknownFields does not exist. The parent message might be cleared.'
  640. )
  641. size = len(self._values)
  642. if index < 0:
  643. index += size
  644. if index < 0 or index >= size:
  645. raise IndexError('index %d out of range'.index)
  646. return UnknownFieldRef(self, index)
  647. def _internal_get(self, index):
  648. return self._values[index]
  649. def __len__(self):
  650. if self._values is None:
  651. raise ValueError(
  652. 'UnknownFields does not exist. The parent message might be cleared.'
  653. )
  654. return len(self._values)
  655. def _add(self, field_number, wire_type, data):
  656. unknown_field = _UnknownField(field_number, wire_type, data)
  657. self._values.append(unknown_field)
  658. return unknown_field
  659. def __iter__(self):
  660. for i in range(len(self)):
  661. yield UnknownFieldRef(self, i)
  662. def _extend(self, other):
  663. if other is None:
  664. return
  665. # pylint: disable=protected-access
  666. self._values.extend(other._values)
  667. def __eq__(self, other):
  668. if self is other:
  669. return True
  670. # Sort unknown fields because their order shouldn't
  671. # affect equality test.
  672. values = list(self._values)
  673. if other is None:
  674. return not values
  675. values.sort()
  676. # pylint: disable=protected-access
  677. other_values = sorted(other._values)
  678. return values == other_values
  679. def _clear(self):
  680. for value in self._values:
  681. # pylint: disable=protected-access
  682. if isinstance(value._data, UnknownFieldSet):
  683. value._data._clear() # pylint: disable=protected-access
  684. self._values = None