Skip to content

Commit 532d7a5

Browse files
committed
BUG27945883: Fixes for failing unittest with MySQL latest versions
Fixes to failing unittest with with MySQL version 5.6.39, 5.7.21 and 8.0.11 v2
1 parent 9dacd15 commit 532d7a5

24 files changed

+319
-212
lines changed

lib/mysql/connector/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -175,8 +175,7 @@ def connect(*args, **kwargs):
175175

176176
if HAVE_CEXT and not use_pure:
177177
return CMySQLConnection(*args, **kwargs)
178-
else:
179-
return MySQLConnection(*args, **kwargs)
178+
return MySQLConnection(*args, **kwargs)
180179
Connect = connect # pylint: disable=C0103
181180

182181
__version_info__ = version.VERSION

lib/mysql/connector/abstracts.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -131,7 +131,7 @@ def _read_option_files(self, config):
131131
config_options[option][1] <= value[1]):
132132
config_options[option] = value
133133
except KeyError:
134-
if group is 'connector_python':
134+
if group == 'connector_python':
135135
raise AttributeError("Unsupported argument "
136136
"'{0}'".format(option))
137137
except KeyError:
@@ -295,6 +295,10 @@ def config(self, **kwargs):
295295
password = self._password
296296
self.set_login(user, password)
297297

298+
# Configure host information
299+
if 'host' in config and config['host']:
300+
self._host = config['host']
301+
298302
# Check network locations
299303
try:
300304
self._port = int(config['port'])
@@ -625,8 +629,7 @@ def python_charset(self):
625629
encoding = CharacterSet.get_info(self._charset_id)[0]
626630
if encoding in ('utf8mb4', 'binary'):
627631
return 'utf8'
628-
else:
629-
return encoding
632+
return encoding
630633

631634
def set_charset_collation(self, charset=None, collation=None):
632635
"""Sets the character set and collation for the current connection
@@ -721,7 +724,7 @@ def connect(self, **kwargs):
721724
arguments are given, it will use the already configured or default
722725
values.
723726
"""
724-
if len(kwargs) > 0:
727+
if kwargs:
725728
self.config(**kwargs)
726729

727730
self.disconnect()
@@ -1049,7 +1052,7 @@ def execute(self, operation, params=(), multi=False):
10491052
pass
10501053

10511054
@abstractmethod
1052-
def executemany(self, operation, seqparams):
1055+
def executemany(self, operation, seq_params):
10531056
"""Execute the given operation multiple times
10541057
10551058
The executemany() method will execute the operation iterating

lib/mysql/connector/authentication.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -228,6 +228,7 @@ def prepare_password(self):
228228
return self._scramble()
229229
elif self._auth_data[0] == self.perform_full_authentication:
230230
return self._full_authentication()
231+
return None
231232

232233
def _full_authentication(self):
233234
"""Returns password as as clear text"""

lib/mysql/connector/catch23.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -63,23 +63,20 @@ def init_bytearray(payload=b'', encoding='utf-8'):
6363
except AttributeError:
6464
raise ValueError("payload must be a str or bytes")
6565

66-
6766
return bytearray(payload)
6867

6968

7069
def isstr(obj):
7170
"""Returns whether a variable is a string"""
7271
if PY2:
7372
return isinstance(obj, basestring)
74-
else:
75-
return isinstance(obj, str)
73+
return isinstance(obj, str)
7674

7775
def isunicode(obj):
7876
"""Returns whether a variable is a of unicode type"""
7977
if PY2:
8078
return isinstance(obj, unicode)
81-
else:
82-
return isinstance(obj, str)
79+
return isinstance(obj, str)
8380

8481

8582
if PY2:

lib/mysql/connector/connection.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -90,12 +90,13 @@ def __init__(self, *args, **kwargs):
9090
self._auth_plugin = None
9191
self._pool_config_version = None
9292

93-
if len(kwargs) > 0:
93+
if kwargs:
9494
try:
9595
self.connect(**kwargs)
9696
except:
9797
# Tidy-up underlying socket on failure
9898
self.close()
99+
self._socket = None
99100
raise
100101

101102
def _do_handshake(self):
@@ -202,6 +203,7 @@ def _auth_switch_request(self, username=None, password=None):
202203
return self._handle_ok(packet)
203204
elif packet[4] == 255:
204205
raise errors.get_exception(packet)
206+
return None
205207

206208
def _get_connection(self, prtcls=None):
207209
"""Get connection based on configuration
@@ -211,15 +213,14 @@ def _get_connection(self, prtcls=None):
211213
212214
Returns subclass of MySQLBaseSocket.
213215
"""
214-
# pylint: disable=R0204
215216
conn = None
216217
if self.unix_socket and os.name != 'nt':
217218
conn = MySQLUnixSocket(unix_socket=self.unix_socket)
218219
else:
219220
conn = MySQLTCPSocket(host=self.server_host,
220221
port=self.server_port,
221222
force_ipv6=self._force_ipv6)
222-
# pylint: enable=R0204
223+
223224
conn.set_connection_timeout(self._connection_timeout)
224225
return conn
225226

@@ -232,15 +233,20 @@ def _open_connection(self):
232233
"""
233234
self._protocol = MySQLProtocol()
234235
self._socket = self._get_connection()
235-
self._socket.open_connection()
236-
self._do_handshake()
237-
self._do_auth(self._user, self._password,
238-
self._database, self._client_flags, self._charset_id,
239-
self._ssl)
240-
self.set_converter_class(self._converter_class)
241-
if self._client_flags & ClientFlag.COMPRESS:
242-
self._socket.recv = self._socket.recv_compressed
243-
self._socket.send = self._socket.send_compressed
236+
try:
237+
self._socket.open_connection()
238+
self._do_handshake()
239+
self._do_auth(self._user, self._password,
240+
self._database, self._client_flags, self._charset_id,
241+
self._ssl)
242+
self.set_converter_class(self._converter_class)
243+
if self._client_flags & ClientFlag.COMPRESS:
244+
self._socket.recv = self._socket.recv_compressed
245+
self._socket.send = self._socket.send_compressed
246+
except:
247+
# close socket
248+
self.close()
249+
raise
244250

245251
def shutdown(self):
246252
"""Shut down connection to MySQL Server.
@@ -252,7 +258,6 @@ def shutdown(self):
252258
self._socket.shutdown()
253259
except (AttributeError, errors.Error):
254260
pass # Getting an exception would mean we are disconnected.
255-
self._socket = None
256261

257262
def close(self):
258263
"""Disconnect from the MySQL server"""
@@ -261,10 +266,9 @@ def close(self):
261266

262267
try:
263268
self.cmd_quit()
264-
self._socket.close_connection()
265269
except (AttributeError, errors.Error):
266270
pass # Getting an exception would mean we are disconnected.
267-
self._socket = None
271+
self._socket.close_connection()
268272

269273
disconnect = close
270274

@@ -451,7 +455,7 @@ def get_row(self, binary=False, columns=None):
451455
Returns a tuple.
452456
"""
453457
(rows, eof) = self.get_rows(count=1, binary=binary, columns=columns)
454-
if len(rows):
458+
if rows:
455459
return (rows[0], eof)
456460
return (None, eof)
457461

lib/mysql/connector/connection_cext.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -69,7 +69,7 @@ def __init__(self, **kwargs):
6969
self.converter = None
7070
super(CMySQLConnection, self).__init__(**kwargs)
7171

72-
if len(kwargs) > 0:
72+
if kwargs:
7373
self.connect(**kwargs)
7474

7575
def _do_handshake(self):
@@ -139,7 +139,7 @@ def in_transaction(self):
139139

140140
def _open_connection(self):
141141
charset_name = CharacterSet.get_info(self._charset_id)[0]
142-
self._cmysql = _mysql_connector.MySQL( # pylint: disable=E1101
142+
self._cmysql = _mysql_connector.MySQL( # pylint: disable=E1101,I1101
143143
buffered=self._buffered,
144144
raw=self._raw,
145145
charset_name=charset_name,
@@ -260,7 +260,6 @@ def get_rows(self, count=None, binary=False, columns=None):
260260
raise AttributeError("count should be 1 or higher, or None")
261261

262262
counter = 0
263-
# pylint: disable=R0204
264263
try:
265264
row = self._cmysql.fetch_row()
266265
while row:
@@ -279,7 +278,7 @@ def get_rows(self, count=None, binary=False, columns=None):
279278
self.free_result()
280279
raise errors.get_mysql_exception(msg=exc.msg, errno=exc.errno,
281280
sqlstate=exc.sqlstate)
282-
# pylint: enable=R0204
281+
283282
return rows
284283

285284
def get_row(self, binary=False, columns=None):

lib/mysql/connector/constants.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -103,10 +103,10 @@ def get_desc(cls, name):
103103
return None
104104

105105
@classmethod
106-
def get_info(cls, num):
106+
def get_info(cls, setid):
107107
"""Get information about given constant"""
108108
for name, info in cls.desc.items():
109-
if info[0] == num:
109+
if info[0] == setid:
110110
return name
111111
return None
112112

@@ -621,7 +621,7 @@ def get_info(cls, setid):
621621
"Character set '{0}' unsupported".format(setid))
622622

623623
@classmethod
624-
def get_desc(cls, setid):
624+
def get_desc(cls, name):
625625
"""Retrieves character set information as string using an ID
626626
627627
Retrieves character set and collation information based on the
@@ -630,7 +630,7 @@ def get_desc(cls, setid):
630630
Returns a tuple.
631631
"""
632632
try:
633-
return "%s/%s" % cls.get_info(setid)
633+
return "%s/%s" % cls.get_info(name)
634634
except:
635635
raise
636636

@@ -763,7 +763,7 @@ def get_desc(cls, name):
763763
raise NotImplementedError
764764

765765
@classmethod
766-
def get_info(cls, number):
766+
def get_info(cls, setid):
767767
raise NotImplementedError
768768

769769
@classmethod

lib/mysql/connector/conversion.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def to_python(self, vtype, value):
9595
except KeyError:
9696
return value
9797

98-
def escape(self, buf):
98+
def escape(self, value):
9999
"""Escape buffer for sending to MySQL"""
100-
return buf
100+
return value
101101

102102
def quote(self, buf):
103103
"""Quote buffer for sending to MySQL"""
@@ -163,14 +163,11 @@ def quote(self, buf):
163163
if PY2:
164164
if isinstance(buf, float):
165165
return repr(buf)
166-
else:
167-
return str(buf)
168-
else:
169-
return str(buf).encode('ascii')
166+
return str(buf)
167+
return str(buf).encode('ascii')
170168
elif isinstance(buf, type(None)):
171169
return bytearray(b"NULL")
172-
else:
173-
return bytearray(b"'" + buf + b"'")
170+
return bytearray(b"'" + buf + b"'")
174171

175172
def to_mysql(self, value):
176173
"""Convert Python data type to MySQL"""
@@ -257,8 +254,7 @@ def _bool_to_mysql(self, value):
257254
"""Convert value to boolean"""
258255
if value:
259256
return 1
260-
else:
261-
return 0
257+
return 0
262258

263259
def _nonetype_to_mysql(self, value):
264260
"""
@@ -358,8 +354,7 @@ def _timedelta_to_mysql(self, value):
358354

359355
if PY2:
360356
return result
361-
else:
362-
return result.encode('ascii')
357+
return result.encode('ascii')
363358

364359
def _decimal_to_mysql(self, value):
365360
"""
@@ -556,8 +551,7 @@ def _JSON_to_python(self, value, dsc=None): # pylint: disable=C0103
556551
num = float(value)
557552
if num.is_integer():
558553
return int(value)
559-
else:
560-
return num
554+
return num
561555
except ValueError:
562556
pass
563557

0 commit comments

Comments
 (0)