店播爬取Python脚本

api_implementation.py 6.6KB

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