Skip to content

Commit 8a88aac

Browse files
committed
Merge branch 'WL7228' into develop
2 parents 22d7e86 + 0b997e1 commit 8a88aac

File tree

8 files changed

+620
-10
lines changed

8 files changed

+620
-10
lines changed

lib/mysql/connector/authentication.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def prepare_password(self):
139139
return b'\x00'
140140
password = self._password
141141

142-
if isinstance(self._password, str):
142+
if PY2:
143+
if isinstance(password, unicode): # pylint: disable=E0602
144+
password = password.encode('utf8')
145+
elif isinstance(password, str):
143146
password = password.encode('utf8')
144147

145148
return password + b'\x00'
@@ -161,7 +164,10 @@ def prepare_password(self):
161164
return b'\x00'
162165
password = self._password
163166

164-
if isinstance(self._password, str):
167+
if PY2:
168+
if isinstance(password, unicode): # pylint: disable=E0602
169+
password = password.encode('utf8')
170+
elif isinstance(password, str):
165171
password = password.encode('utf8')
166172

167173
return password + b'\x00'

lib/mysql/connector/connection.py

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,28 @@
2424
"""Implementing communication with MySQL servers.
2525
"""
2626

27+
from io import IOBase
2728
import os
28-
import time
2929
import re
30-
from io import IOBase
30+
import time
3131

32-
from .network import MySQLUnixSocket, MySQLTCPSocket
32+
from . import errors
33+
from .authentication import get_auth_plugin
34+
from .catch23 import PY2, isstr
3335
from .constants import (
3436
ClientFlag, ServerCmd, CharacterSet, ServerFlag,
3537
flag_is_set, ShutdownType, NET_BUFFER_LENGTH
3638
)
3739
from .conversion import MySQLConverterBase, MySQLConverter
38-
from .protocol import MySQLProtocol
39-
from . import errors
40-
from .utils import int4store
4140
from .cursor import (
4241
CursorBase, MySQLCursor, MySQLCursorRaw,
4342
MySQLCursorBuffered, MySQLCursorBufferedRaw, MySQLCursorPrepared,
4443
MySQLCursorDict, MySQLCursorBufferedDict, MySQLCursorNamedTuple,
4544
MySQLCursorBufferedNamedTuple)
46-
from .authentication import get_auth_plugin
47-
from .catch23 import PY2, isstr
45+
from .network import MySQLUnixSocket, MySQLTCPSocket
46+
from .optionfiles import MySQLOptionsParser
47+
from .protocol import MySQLProtocol
48+
from .utils import int4store
4849

4950
DEFAULT_CONFIGURATION = {
5051
'database': None,
@@ -233,6 +234,59 @@ def _auth_switch_request(self, username=None, password=None):
233234
elif packet[4] == 255:
234235
raise errors.get_exception(packet)
235236

237+
def _read_option_files(self, config):
238+
"""
239+
Read option files for connection parameters.
240+
241+
Checks if connection arguments contain option file arguments, and then
242+
reads option files accordingly.
243+
"""
244+
if 'option_files' in config:
245+
try:
246+
if isinstance(config['option_groups'], str):
247+
config['option_groups'] = [config['option_groups']]
248+
groups = config['option_groups']
249+
del config['option_groups']
250+
except KeyError:
251+
groups = ['client', 'connector_python']
252+
253+
if isinstance(config['option_files'], str):
254+
config['option_files'] = [config['option_files']]
255+
option_parser = MySQLOptionsParser(list(config['option_files']),
256+
keep_dashes=False)
257+
del config['option_files']
258+
259+
config_from_file = option_parser.get_groups_as_dict_with_priority(
260+
*groups)
261+
config_options = {}
262+
for group in groups:
263+
try:
264+
for option, value in config_from_file[group].items():
265+
try:
266+
if option == 'socket':
267+
option = 'unix_socket'
268+
# pylint: disable=W0104
269+
DEFAULT_CONFIGURATION[option]
270+
# pylint: enable=W0104
271+
272+
if (option not in config_options or
273+
config_options[option][1] <= value[1]):
274+
config_options[option] = value
275+
except KeyError:
276+
if group is 'connector_python':
277+
raise AttributeError("Unsupported argument "
278+
"'{0}'".format(option))
279+
except KeyError:
280+
continue
281+
282+
for option, value in config_options.items():
283+
if option not in config:
284+
try:
285+
config[option] = eval(value[0]) # pylint: disable=W0123
286+
except (NameError, SyntaxError):
287+
config[option] = value[0]
288+
return config
289+
236290
def config(self, **kwargs):
237291
"""Configure the MySQL Connection
238292
@@ -244,6 +298,9 @@ def config(self, **kwargs):
244298
if 'dsn' in config:
245299
raise errors.NotSupportedError("Data source name is not supported")
246300

301+
# Read option files
302+
self._read_option_files(config)
303+
247304
# Configure how we handle MySQL warnings
248305
try:
249306
self.get_warnings = config['get_warnings']

0 commit comments

Comments
 (0)