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 except ImportError: 64 if _proto_extension_modules_exist_in_build: 65 if sys.version_info[0] >= 3: # Python 3 defaults to C++ impl v2. 66 _api_version = 2 67 # TODO(b/17427486): Make Python 2 default to C++ impl v2. 68 69_default_implementation_type = ( 70 'python' if _api_version <= 0 else 'cpp') 71 72# This environment variable can be used to switch to a certain implementation 73# of the Python API, overriding the compile-time constants in the 74# _api_implementation module. Right now only 'python' and 'cpp' are valid 75# values. Any other value will be ignored. 76_implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', 77 _default_implementation_type) 78 79if _implementation_type != 'python': 80 _implementation_type = 'cpp' 81 82if 'PyPy' in sys.version and _implementation_type == 'cpp': 83 warnings.warn('PyPy does not work yet with cpp protocol buffers. ' 84 'Falling back to the python implementation.') 85 _implementation_type = 'python' 86 87# This environment variable can be used to switch between the two 88# 'cpp' implementations, overriding the compile-time constants in the 89# _api_implementation module. Right now only '2' is supported. Any other 90# value will cause an error to be raised. 91_implementation_version_str = os.getenv( 92 'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', '2') 93 94if _implementation_version_str != '2': 95 raise ValueError( 96 'unsupported PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION: "' + 97 _implementation_version_str + '" (supported versions: 2)' 98 ) 99 100_implementation_version = int(_implementation_version_str) 101 102 103# Usage of this function is discouraged. Clients shouldn't care which 104# implementation of the API is in use. Note that there is no guarantee 105# that differences between APIs will be maintained. 106# Please don't use this function if possible. 107def Type(): 108 return _implementation_type 109 110 111# See comment on 'Type' above. 112def Version(): 113 return _implementation_version 114