| |
| |
| |
| |
| |
| |
|
|
| """Determine which implementation of the protobuf API is used in this process. |
| """ |
|
|
| import importlib |
| import os |
| import sys |
| import warnings |
|
|
| _GOOGLE3_PYTHON_UPB_DEFAULT = True |
|
|
|
|
| def _ApiVersionToImplementationType(api_version): |
| if api_version == 2: |
| return 'cpp' |
| if api_version == 1: |
| raise ValueError('api_version=1 is no longer supported.') |
| if api_version == 0: |
| return 'python' |
| return None |
|
|
|
|
| _implementation_type = None |
| try: |
| |
| from google.protobuf.internal import _api_implementation |
| |
| |
| _implementation_type = _ApiVersionToImplementationType( |
| _api_implementation.api_version) |
| except ImportError: |
| pass |
|
|
|
|
| def _CanImport(mod_name): |
| try: |
| mod = importlib.import_module(mod_name) |
| |
| if not mod: |
| raise ImportError(mod_name + ' import succeeded but was None') |
| return True |
| except ImportError: |
| return False |
|
|
|
|
| if _implementation_type is None: |
| if _CanImport('google._upb._message'): |
| _implementation_type = 'upb' |
| elif _CanImport('google.protobuf.pyext._message'): |
| _implementation_type = 'cpp' |
| else: |
| _implementation_type = 'python' |
|
|
|
|
| |
| |
| |
| |
| _implementation_type = os.getenv('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION', |
| _implementation_type) |
|
|
| if _implementation_type not in ('python', 'cpp', 'upb'): |
| raise ValueError('PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION {0} is not ' |
| 'supported. Please set to \'python\', \'cpp\' or ' |
| '\'upb\'.'.format(_implementation_type)) |
|
|
| if 'PyPy' in sys.version and _implementation_type == 'cpp': |
| warnings.warn('PyPy does not work yet with cpp protocol buffers. ' |
| 'Falling back to the python implementation.') |
| _implementation_type = 'python' |
|
|
| _c_module = None |
|
|
| if _implementation_type == 'cpp': |
| try: |
| |
| from google.protobuf.pyext import _message |
| sys.modules['google3.net.proto2.python.internal.cpp._message'] = _message |
| _c_module = _message |
| del _message |
| except ImportError: |
| |
| warnings.warn( |
| 'Selected implementation cpp is not available.') |
| pass |
|
|
| if _implementation_type == 'upb': |
| try: |
| |
| from google._upb import _message |
| _c_module = _message |
| del _message |
| except ImportError: |
| warnings.warn('Selected implementation upb is not available. ' |
| 'Falling back to the python implementation.') |
| _implementation_type = 'python' |
| pass |
|
|
| |
| try: |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from google.protobuf import enable_deterministic_proto_serialization |
| _python_deterministic_proto_serialization = True |
| except ImportError: |
| _python_deterministic_proto_serialization = False |
|
|
|
|
| |
| |
| |
| |
| def Type(): |
| return _implementation_type |
|
|
|
|
| |
| |
| def Version(): |
| return 2 |
|
|
|
|
| |
| def IsPythonDefaultSerializationDeterministic(): |
| return _python_deterministic_proto_serialization |
|
|