Skip to content

Commit 4e20086

Browse files
committed
Detect the usage of unsupported Protobuf versions
1 parent 560148b commit 4e20086

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

lib/mysqlx/protobuf/__init__.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"Mysqlx.Connection.Compression"),
6161
)
6262

63+
PROTOBUF_VERSION = None
6364
PROTOBUF_REPEATED_TYPES = [list]
6465

6566
try:
@@ -74,19 +75,7 @@
7475
from ..helpers import encode_to_bytes
7576

7677
try:
77-
from . import mysqlx_connection_pb2
78-
from . import mysqlx_crud_pb2
79-
from . import mysqlx_cursor_pb2
80-
from . import mysqlx_datatypes_pb2
81-
from . import mysqlx_expect_pb2
82-
from . import mysqlx_expr_pb2
83-
from . import mysqlx_notice_pb2
84-
from . import mysqlx_pb2
85-
from . import mysqlx_prepare_pb2
86-
from . import mysqlx_resultset_pb2
87-
from . import mysqlx_session_pb2
88-
from . import mysqlx_sql_pb2
89-
78+
from google import protobuf
9079
from google.protobuf import descriptor_database
9180
from google.protobuf import descriptor_pb2
9281
from google.protobuf import descriptor_pool
@@ -101,6 +90,22 @@
10190
pass
10291

10392
PROTOBUF_REPEATED_TYPES.append(RepeatedCompositeFieldContainer)
93+
if hasattr(protobuf, "__version__"):
94+
# Only Protobuf versions >=3.0.0 provide `__version__`
95+
PROTOBUF_VERSION = protobuf.__version__
96+
97+
from . import mysqlx_connection_pb2
98+
from . import mysqlx_crud_pb2
99+
from . import mysqlx_cursor_pb2
100+
from . import mysqlx_datatypes_pb2
101+
from . import mysqlx_expect_pb2
102+
from . import mysqlx_expr_pb2
103+
from . import mysqlx_notice_pb2
104+
from . import mysqlx_pb2
105+
from . import mysqlx_prepare_pb2
106+
from . import mysqlx_resultset_pb2
107+
from . import mysqlx_session_pb2
108+
from . import mysqlx_sql_pb2
104109

105110
# Dictionary with all messages descriptors
106111
_MESSAGES = {}
@@ -236,11 +241,13 @@ def parse_server_message(msg_type, payload):
236241
msg = _mysqlxpb_pure.new_message(msg_type_name)
237242
msg.ParseFromString(payload)
238243
return msg
239-
except ImportError as err:
244+
except (ImportError, SyntaxError, TypeError) as err:
240245
HAVE_PROTOBUF = False
241-
HAVE_PROTOBUF_ERROR = err
246+
HAVE_PROTOBUF_ERROR = err if PROTOBUF_VERSION is not None \
247+
else "Protobuf >=3.0.0 is required"
242248
if not HAVE_MYSQLXPB_CEXT:
243-
raise ImportError("Protobuf is not available: {}".format(err))
249+
raise ImportError("Protobuf is not available: {}"
250+
"".format(HAVE_PROTOBUF_ERROR))
244251

245252
CRUD_PREPARE_MAPPING = {
246253
"Mysqlx.ClientMessages.Type.CRUD_FIND": (

0 commit comments

Comments
 (0)