api_implementation.py 4.5 KB

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