1
# Protocol Buffers - Google's data interchange format
2
# Copyright 2008 Google Inc. All rights reserved.
3
# https://developers.google.com/protocol-buffers/
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are
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
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.
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.
31
"""Determine which implementation of the protobuf API is used in this process.
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
45
_api_version = -1 # Unspecified by compiler flags.
48
raise ValueError('api_version=1 is no longer supported.')
51
_default_implementation_type = ('cpp' if _api_version > 0 else 'python')
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)
61
if _implementation_type != 'python':
62
_implementation_type = 'cpp'
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'
70
# Detect if serialization should be deterministic by default
72
# The presence of this module in a build allows the proto implementation to
73
# be upgraded merely via build deps.
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'`.
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.
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
88
_python_deterministic_proto_serialization = False
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.
96
return _implementation_type
99
def _SetType(implementation_type):
100
"""Never use! Only for protobuf benchmark."""
101
global _implementation_type
102
_implementation_type = implementation_type
105
# See comment on 'Type' above.
110
# For internal use only
111
def IsPythonDefaultSerializationDeterministic():
112
return _python_deterministic_proto_serialization