Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/internal/api_implementation.py: 54%

69 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

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 importlib 

35import os 

36import sys 

37import warnings 

38 

39 

40def _ApiVersionToImplementationType(api_version): 

41 if api_version == 2: 

42 return 'cpp' 

43 if api_version == 1: 

44 raise ValueError('api_version=1 is no longer supported.') 

45 if api_version == 0: 

46 return 'python' 

47 return None 

48 

49 

50_implementation_type = None 

51try: 

52 # pylint: disable=g-import-not-at-top 

53 from google.protobuf.internal import _api_implementation 

54 # The compile-time constants in the _api_implementation module can be used to 

55 # switch to a certain implementation of the Python API at build time. 

56 _implementation_type = _ApiVersionToImplementationType( 

57 _api_implementation.api_version) 

58except ImportError: 

59 pass # Unspecified by compiler flags. 

60 

61 

62def _CanImport(mod_name): 

63 try: 

64 mod = importlib.import_module(mod_name) 

65 # Work around a known issue in the classic bootstrap .par import hook. 

66 if not mod: 

67 raise ImportError(mod_name + ' import succeeded but was None') 

68 return True 

69 except ImportError: 

70 return False 

71 

72 

73if _implementation_type is None: 

74 if _CanImport('google._upb._message'): 

75 _implementation_type = 'upb' 

76 elif _CanImport('google.protobuf.pyext._message'): 

77 _implementation_type = 'cpp' 

78 else: 

79 _implementation_type = 'python' 

80 

81 

82# This environment variable can be used to switch to a certain implementation 

83# of the Python API, overriding the compile-time constants in the 

84# _api_implementation module. Right now only 'python', 'cpp' and 'upb' are 

85# valid values. Any other value will raise error. 

86_implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', 

87 _implementation_type) 

88 

89if _implementation_type not in ('python', 'cpp', 'upb'): 

90 raise ValueError('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION {0} is not ' 

91 'supported. Please set to \'python\', \'cpp\' or ' 

92 '\'upb\'.'.format(_implementation_type)) 

93 

94if 'PyPy' in sys.version and _implementation_type == 'cpp': 

95 warnings.warn('PyPy does not work yet with cpp protocol buffers. ' 

96 'Falling back to the python implementation.') 

97 _implementation_type = 'python' 

98 

99_c_module = None 

100 

101if _implementation_type == 'cpp': 

102 try: 

103 # pylint: disable=g-import-not-at-top 

104 from google.protobuf.pyext import _message 

105 sys.modules['google3.net.proto2.python.internal.cpp._message'] = _message 

106 _c_module = _message 

107 del _message 

108 except ImportError: 

109 # TODO(jieluo): fail back to python 

110 warnings.warn( 

111 'Selected implementation cpp is not available.') 

112 pass 

113 

114if _implementation_type == 'upb': 

115 try: 

116 # pylint: disable=g-import-not-at-top 

117 from google._upb import _message 

118 _c_module = _message 

119 del _message 

120 except ImportError: 

121 warnings.warn('Selected implementation upb is not available. ' 

122 'Falling back to the python implementation.') 

123 _implementation_type = 'python' 

124 pass 

125 

126# Detect if serialization should be deterministic by default 

127try: 

128 # The presence of this module in a build allows the proto implementation to 

129 # be upgraded merely via build deps. 

130 # 

131 # NOTE: Merely importing this automatically enables deterministic proto 

132 # serialization for C++ code, but we still need to export it as a boolean so 

133 # that we can do the same for `_implementation_type == 'python'`. 

134 # 

135 # NOTE2: It is possible for C++ code to enable deterministic serialization by 

136 # default _without_ affecting Python code, if the C++ implementation is not in 

137 # use by this module. That is intended behavior, so we don't actually expose 

138 # this boolean outside of this module. 

139 # 

140 # pylint: disable=g-import-not-at-top,unused-import 

141 from google.protobuf import enable_deterministic_proto_serialization 

142 _python_deterministic_proto_serialization = True 

143except ImportError: 

144 _python_deterministic_proto_serialization = False 

145 

146 

147# Usage of this function is discouraged. Clients shouldn't care which 

148# implementation of the API is in use. Note that there is no guarantee 

149# that differences between APIs will be maintained. 

150# Please don't use this function if possible. 

151def Type(): 

152 return _implementation_type 

153 

154 

155# See comment on 'Type' above. 

156# TODO(jieluo): Remove the API, it returns a constant. b/228102101 

157def Version(): 

158 return 2 

159 

160 

161# For internal use only 

162def IsPythonDefaultSerializationDeterministic(): 

163 return _python_deterministic_proto_serialization