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 34import os 35import warnings 36import sys 37 38try: 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 _proto_extension_modules_exist_in_build = True 45except ImportError: 46 _api_version = -1 # Unspecified by compiler flags. 47 _proto_extension_modules_exist_in_build = False 48 49if _api_version == 1: 50 raise ValueError('api_version=1 is no longer supported.') 51if _api_version < 0: # Still unspecified? 52 try: 53 # The presence of this module in a build allows the proto implementation to 54 # be upgraded merely via build deps rather than a compiler flag or the 55 # runtime environment variable. 56 # pylint: disable=g-import-not-at-top 57 from google.protobuf import _use_fast_cpp_protos 58 # Work around a known issue in the classic bootstrap .par import hook. 59 if not _use_fast_cpp_protos: 60 raise ImportError('_use_fast_cpp_protos import succeeded but was None') 61 del _use_fast_cpp_protos 62 _api_version = 2 63 # Can not import both use_fast_cpp_protos and use_pure_python. 64 from google.protobuf import use_pure_python 65 raise RuntimeError( 66 'Conflict depend on both use_fast_cpp_protos and use_pure_python') 67 except ImportError: 68 try: 69 # pylint: disable=g-import-not-at-top 70 from google.protobuf import use_pure_python 71 del use_pure_python # Avoids a pylint error and namespace pollution. 72 _api_version = 0 73 except ImportError: 74 # TODO(b/74017912): It's unsafe to enable :use_fast_cpp_protos by default; 75 # it can cause data loss if you have any Python-only extensions to any 76 # message passed back and forth with C++ code. 77 # 78 # TODO(b/17427486): Once that bug is fixed, we want to make both Python 2 79 # and Python 3 default to `_api_version = 2` (C++ implementation V2). 80 pass 81 82_default_implementation_type = ( 83 'python' if _api_version <= 0 else 'cpp') 84 85# This environment variable can be used to switch to a certain implementation 86# of the Python API, overriding the compile-time constants in the 87# _api_implementation module. Right now only 'python' and 'cpp' are valid 88# values. Any other value will be ignored. 89_implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', 90 _default_implementation_type) 91 92if _implementation_type != 'python': 93 _implementation_type = 'cpp' 94 95if 'PyPy' in sys.version and _implementation_type == 'cpp': 96 warnings.warn('PyPy does not work yet with cpp protocol buffers. ' 97 'Falling back to the python implementation.') 98 _implementation_type = 'python' 99 100# This environment variable can be used to switch between the two 101# 'cpp' implementations, overriding the compile-time constants in the 102# _api_implementation module. Right now only '2' is supported. Any other 103# value will cause an error to be raised. 104_implementation_version_str = os.getenv( 105 'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', '2') 106 107if _implementation_version_str != '2': 108 raise ValueError( 109 'unsupported PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION: "' + 110 _implementation_version_str + '" (supported versions: 2)' 111 ) 112 113_implementation_version = int(_implementation_version_str) 114 115 116# Detect if serialization should be deterministic by default 117try: 118 # The presence of this module in a build allows the proto implementation to 119 # be upgraded merely via build deps. 120 # 121 # NOTE: Merely importing this automatically enables deterministic proto 122 # serialization for C++ code, but we still need to export it as a boolean so 123 # that we can do the same for `_implementation_type == 'python'`. 124 # 125 # NOTE2: It is possible for C++ code to enable deterministic serialization by 126 # default _without_ affecting Python code, if the C++ implementation is not in 127 # use by this module. That is intended behavior, so we don't actually expose 128 # this boolean outside of this module. 129 # 130 # pylint: disable=g-import-not-at-top,unused-import 131 from google.protobuf import enable_deterministic_proto_serialization 132 _python_deterministic_proto_serialization = True 133except ImportError: 134 _python_deterministic_proto_serialization = False 135 136 137# Usage of this function is discouraged. Clients shouldn't care which 138# implementation of the API is in use. Note that there is no guarantee 139# that differences between APIs will be maintained. 140# Please don't use this function if possible. 141def Type(): 142 return _implementation_type 143 144 145def _SetType(implementation_type): 146 """Never use! Only for protobuf benchmark.""" 147 global _implementation_type 148 _implementation_type = implementation_type 149 150 151# See comment on 'Type' above. 152def Version(): 153 return _implementation_version 154 155 156# For internal use only 157def IsPythonDefaultSerializationDeterministic(): 158 return _python_deterministic_proto_serialization 159