Debian Codegraph @

proto/marshal/compat.py

raw
1 # Copyright 2018 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # https://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # This file pulls in the container types from internal protocol buffers,
16 # and exports the types available.
17 #
18 # If the C extensions were not installed, then their container types will
19 # not be included.
20
21 from google.protobuf.internal import containers
22
23 # Import all message types to ensure that pyext types are recognized
24 # when upb types exist. Conda's protobuf defaults to pyext despite upb existing.
25 # See https://github.com/googleapis/proto-plus-python/issues/470
26 try:
27 from google._upb import _message as _message_upb
28 except ImportError:
29 _message_upb = None
30
31 try:
32 from google.protobuf.pyext import _message as _message_pyext
33 except ImportError:
34 _message_pyext = None
35
36
37 repeated_composite_types = (containers.RepeatedCompositeFieldContainer,)
38 repeated_scalar_types = (containers.RepeatedScalarFieldContainer,)
39 map_composite_types = (containers.MessageMap,)
40
41 # In `proto/marshal.py`, for compatibility with protobuf 5.x,
42 # we'll use `map_composite_type_names` to check whether
43 # the name of the class of a protobuf type is
44 # `MessageMapContainer`, and, if `True`, return a MapComposite.
45 # See https://github.com/protocolbuffers/protobuf/issues/16596
46 map_composite_type_names = ("MessageMapContainer",)
47
48 for message in [_message_upb, _message_pyext]:
49 if message:
50 repeated_composite_types += (message.RepeatedCompositeContainer,)
51 repeated_scalar_types += (message.RepeatedScalarContainer,)
52
53 try:
54 map_composite_types += (message.MessageMapContainer,)
55 except AttributeError:
56 # The `MessageMapContainer` attribute is not available in Protobuf 5.x+
57 pass
58
59 __all__ = (
60 "repeated_composite_types",
61 "repeated_scalar_types",
62 "map_composite_types",
63 "map_composite_type_names",
64 )