protobuf/python/google/protobuf/internal/api_implementation.py
Go to the documentation of this file.
1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc. All rights reserved.
3 # https://developers.google.com/protocol-buffers/
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 """Determine which implementation of the protobuf API is used in this process.
32 """
33 
34 import os
35 import sys
36 import warnings
37 
38 try:
39  # pylint: disable=g-import-not-at-top
40  from google.protobuf.internal import _api_implementation
41  # The compile-time constants in the _api_implementation module can be used to
42  # switch to a certain implementation of the Python API at build time.
43  _api_version = _api_implementation.api_version
44 except ImportError:
45  _api_version = -1 # Unspecified by compiler flags.
46 
47 if _api_version == 1:
48  raise ValueError('api_version=1 is no longer supported.')
49 
50 
51 _default_implementation_type = ('cpp' if _api_version > 0 else 'python')
52 
53 
54 # This environment variable can be used to switch to a certain implementation
55 # of the Python API, overriding the compile-time constants in the
56 # _api_implementation module. Right now only 'python' and 'cpp' are valid
57 # values. Any other value will be ignored.
58 _implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION',
59  _default_implementation_type)
60 
61 if _implementation_type != 'python':
62  _implementation_type = 'cpp'
63 
64 if 'PyPy' in sys.version and _implementation_type == 'cpp':
65  warnings.warn('PyPy does not work yet with cpp protocol buffers. '
66  'Falling back to the python implementation.')
67  _implementation_type = 'python'
68 
69 
70 # Detect if serialization should be deterministic by default
71 try:
72  # The presence of this module in a build allows the proto implementation to
73  # be upgraded merely via build deps.
74  #
75  # NOTE: Merely importing this automatically enables deterministic proto
76  # serialization for C++ code, but we still need to export it as a boolean so
77  # that we can do the same for `_implementation_type == 'python'`.
78  #
79  # NOTE2: It is possible for C++ code to enable deterministic serialization by
80  # default _without_ affecting Python code, if the C++ implementation is not in
81  # use by this module. That is intended behavior, so we don't actually expose
82  # this boolean outside of this module.
83  #
84  # pylint: disable=g-import-not-at-top,unused-import
85  from google.protobuf import enable_deterministic_proto_serialization
86  _python_deterministic_proto_serialization = True
87 except ImportError:
88  _python_deterministic_proto_serialization = False
89 
90 
91 # Usage of this function is discouraged. Clients shouldn't care which
92 # implementation of the API is in use. Note that there is no guarantee
93 # that differences between APIs will be maintained.
94 # Please don't use this function if possible.
95 def Type():
96  return _implementation_type
97 
98 
99 def _SetType(implementation_type):
100  """Never use! Only for protobuf benchmark."""
101  global _implementation_type
102  _implementation_type = implementation_type
103 
104 
105 # See comment on 'Type' above.
106 def Version():
107  return 2
108 
109 
110 # For internal use only
112  return _python_deterministic_proto_serialization
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf.internal.api_implementation.IsPythonDefaultSerializationDeterministic
def IsPythonDefaultSerializationDeterministic()
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/api_implementation.py:152
google::protobuf.internal
Definition: third_party/bloaty/third_party/protobuf/python/google/protobuf/internal/__init__.py:1
google::protobuf.internal.api_implementation.Type
def Type()
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/api_implementation.py:136
google::protobuf.internal.api_implementation.Version
def Version()
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/api_implementation.py:147
google::protobuf.internal.api_implementation._SetType
def _SetType(implementation_type)
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/api_implementation.py:140


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:31