Skip to content

Commit 165106d

Browse files
committed
WL11614: Enable C extension by default
This WL changes the default value of connection option use_pure from True to False in the mysql.connector package, which enables the usage of the C extension by default. It also implements the mechanism for enabling the usage of pure Python in the mysqlx package. Tests were added for regression.
1 parent 5319228 commit 165106d

File tree

13 files changed

+182
-85
lines changed

13 files changed

+182
-85
lines changed

cpyint

docs/mysqlx/tutorials/getting_started.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ If ``ssl-ca`` option is set, only the following SSL Modes are allowed:
6666
'ssl-ca': '/path/to/ca.cert'
6767
})
6868
69+
Connector/Python has a C extension for `Protobuf <https://developers.google.com/protocol-buffers/>`_ message serialization, this C extension is enabled by default if available. It can be disabled by setting the ``use-pure`` option to :data:`True`.
70+
71+
.. code-block:: python
72+
73+
session = mysqlx.get_session('mysqlx://root:@localhost:33060?use-pure=true')
74+
session = mysqlx.get_session(host='localhost', port=33060, user='root', password='', use_pure=True)
75+
session = mysqlx.get_session({
76+
'host': 'localhost',
77+
'port': 33060,
78+
'user': 'root',
79+
'password': '',
80+
'use-pure': True
81+
})
82+
6983
The :func:`mysqlx.Schema.get_schema()` method returns a :class:`mysqlx.Schema` object. We can use this :class:`mysqlx.Schema` object to access collections and tables. X DevAPI's ability to chain all object constructions, enables you to get to the schema object in one line. For example:
7084

7185
.. code-block:: python

lib/mysql/connector/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -170,7 +170,7 @@ def connect(*args, **kwargs):
170170
# No pooling
171171
pass
172172

173-
use_pure = kwargs.setdefault('use_pure', True)
173+
use_pure = kwargs.setdefault('use_pure', False)
174174

175175
try:
176176
del kwargs['use_pure']

lib/mysql/connector/pooling.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def set_config(self, **kwargs):
195195
with CONNECTION_POOL_LOCK:
196196
try:
197197
test_cnx = MySQLConnection()
198+
if "use_pure" in kwargs:
199+
del kwargs["use_pure"]
198200
test_cnx.config(**kwargs)
199201
self._cnx_config = kwargs
200202
self._config_version = uuid4()

lib/mysqlx/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License, version 2.0, as
@@ -54,7 +54,7 @@
5454
_PRIORITY = re.compile(r'^\(address=(.+),priority=(\d+)\)$', re.VERBOSE)
5555
_SSL_OPTS = ["ssl-cert", "ssl-ca", "ssl-key", "ssl-crl"]
5656
_SESS_OPTS = _SSL_OPTS + ["user", "password", "schema", "host", "port",
57-
"routers", "socket", "ssl-mode", "auth"]
57+
"routers", "socket", "ssl-mode", "auth", "use-pure"]
5858

5959
def _parse_address_list(path):
6060
"""Parses a list of host, port pairs
@@ -125,13 +125,19 @@ def _parse_connection_uri(uri):
125125
settings.update(_parse_address_list(host))
126126

127127
for key, val in parse_qsl(query_str, True):
128-
opt = key.lower()
128+
opt = key.replace("_", "-").lower()
129129
if opt in settings:
130130
raise InterfaceError("Duplicate option '{0}'.".format(key))
131131
if opt in _SSL_OPTS:
132132
settings[opt] = unquote(val.strip("()"))
133133
else:
134-
settings[opt] = val.lower()
134+
val_str = val.lower()
135+
if val_str in ("1", "true"):
136+
settings[opt] = True
137+
elif val_str in ("0", "false"):
138+
settings[opt] = False
139+
else:
140+
settings[opt] = val_str
135141
return settings
136142

137143
def _validate_settings(settings):
@@ -227,7 +233,8 @@ def _get_connection_settings(*args, **kwargs):
227233
if isinstance(args[0], STRING_TYPES):
228234
settings = _parse_connection_uri(args[0])
229235
elif isinstance(args[0], dict):
230-
settings.update(args[0])
236+
for key, val in args[0].items():
237+
settings[key.replace("_", "-")] = val
231238
elif kwargs:
232239
settings.update(kwargs)
233240
for key in settings:

lib/mysqlx/connection.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from .protocol import Protocol, MessageReaderWriter
5353
from .result import Result, RowResult, DocResult
5454
from .statement import SqlStatement, AddStatement, quote_identifier
55+
from .protobuf import Protobuf
5556

5657

5758
_DROP_DATABASE_QUERY = "DROP DATABASE IF EXISTS `{0}`"
@@ -649,10 +650,23 @@ class Session(object):
649650
settings (dict): Connection data used to connect to the database.
650651
"""
651652
def __init__(self, settings):
653+
self.use_pure = settings.get("use-pure", Protobuf.use_pure)
652654
self._settings = settings
653655
self._connection = Connection(self._settings)
654656
self._connection.connect()
655657

658+
@property
659+
def use_pure(self):
660+
"""bool: `True` to use pure Python Protobuf implementation.
661+
"""
662+
return Protobuf.use_pure
663+
664+
@use_pure.setter
665+
def use_pure(self, value):
666+
if not isinstance(value, bool):
667+
raise ProgrammingError("'use_pure' option should be True or False")
668+
Protobuf.set_use_pure(value)
669+
656670
def is_open(self):
657671
"""Returns `True` if the session is open.
658672

0 commit comments

Comments
 (0)