blob: ffcf751167a5a973fa2e48f06a7b2920802fa494 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001# 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 sys
36
37try:
38 # pylint: disable=g-import-not-at-top
39 from google.protobuf.internal import _api_implementation
40 # The compile-time constants in the _api_implementation module can be used to
41 # switch to a certain implementation of the Python API at build time.
42 _api_version = _api_implementation.api_version
43 _proto_extension_modules_exist_in_build = True
44except ImportError:
45 _api_version = -1 # Unspecified by compiler flags.
46 _proto_extension_modules_exist_in_build = False
47
48if _api_version == 1:
49 raise ValueError('api_version=1 is no longer supported.')
50if _api_version < 0: # Still unspecified?
51 try:
52 # The presence of this module in a build allows the proto implementation to
53 # be upgraded merely via build deps rather than a compiler flag or the
54 # runtime environment variable.
55 # pylint: disable=g-import-not-at-top
56 from google.protobuf import _use_fast_cpp_protos
57 # Work around a known issue in the classic bootstrap .par import hook.
58 if not _use_fast_cpp_protos:
59 raise ImportError('_use_fast_cpp_protos import succeeded but was None')
60 del _use_fast_cpp_protos
61 _api_version = 2
62 except ImportError:
63 if _proto_extension_modules_exist_in_build:
64 if sys.version_info[0] >= 3: # Python 3 defaults to C++ impl v2.
65 _api_version = 2
66 # TODO(b/17427486): Make Python 2 default to C++ impl v2.
67
68_default_implementation_type = (
69 'python' if _api_version <= 0 else 'cpp')
70
71# This environment variable can be used to switch to a certain implementation
72# of the Python API, overriding the compile-time constants in the
73# _api_implementation module. Right now only 'python' and 'cpp' are valid
74# values. Any other value will be ignored.
75_implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION',
76 _default_implementation_type)
77
78if _implementation_type != 'python':
79 _implementation_type = 'cpp'
80
81# This environment variable can be used to switch between the two
82# 'cpp' implementations, overriding the compile-time constants in the
83# _api_implementation module. Right now only '2' is supported. Any other
84# value will cause an error to be raised.
85_implementation_version_str = os.getenv(
86 'PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION', '2')
87
88if _implementation_version_str != '2':
89 raise ValueError(
90 'unsupported PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION_VERSION: "' +
91 _implementation_version_str + '" (supported versions: 2)'
92 )
93
94_implementation_version = int(_implementation_version_str)
95
96
97# Usage of this function is discouraged. Clients shouldn't care which
98# implementation of the API is in use. Note that there is no guarantee
99# that differences between APIs will be maintained.
100# Please don't use this function if possible.
101def Type():
102 return _implementation_type
103
104
105# See comment on 'Type' above.
106def Version():
107 return _implementation_version