Skip to content

Commit c2e6c5d

Browse files
committed
BUG27794178: Using use_pure=False should raise an error if cext is not available
The pure Python implementation is used instead of raising an error when the connection option `pure_python` is set to False and the C extension is not available. Tests were added/changed.
1 parent bb054a9 commit c2e6c5d

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

lib/mysql/connector/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ def connect(*args, **kwargs):
160160
# No pooling
161161
pass
162162

163-
use_pure = kwargs.setdefault('use_pure', False)
164-
165-
try:
166-
del kwargs['use_pure']
167-
except KeyError:
168-
# Just making sure 'use_pure' is not kwargs
169-
pass
163+
# Use C Extension by default
164+
use_pure = kwargs.get('use_pure', False)
165+
if 'use_pure' in kwargs:
166+
del kwargs['use_pure'] # Remove 'use_pure' from kwargs
167+
if not use_pure and not HAVE_CEXT:
168+
raise ImportError("MySQL Connector/Python C Extension not "
169+
"available")
170170

171171
if HAVE_CEXT and not use_pure:
172172
return CMySQLConnection(*args, **kwargs)

tests/issues/test_bug21879914.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -52,6 +52,8 @@
5252

5353
OPTION_FILE = os.path.join('tests', 'data', 'option_files', 'my.cnf')
5454

55+
56+
@unittest.skipIf(not CMySQLConnection, "C Extension not available")
5557
class Bug21879914(tests.MySQLConnectorTests):
5658

5759
def test_ssl_cipher_in_option_file(self):

tests/test_bugs.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4345,6 +4345,7 @@ def test_pure_verify_server_certifcate(self):
43454345

43464346
self._verify_cert(config)
43474347

4348+
@unittest.skipIf(not CMySQLConnection, ERR_NO_CEXT)
43484349
def test_cext_verify_server_certifcate(self):
43494350
config = self.config.copy()
43504351
config['use_pure'] = False
@@ -4495,6 +4496,7 @@ def test_ssl_disabled_pure(self):
44954496
self.config['use_pure'] = True
44964497
self._test_ssl_modes()
44974498

4499+
@unittest.skipIf(not CMySQLConnection, ERR_NO_CEXT)
44984500
def test_ssl_disabled_cext(self):
44994501
self.config['use_pure'] = False
45004502
self._test_ssl_modes()
@@ -4940,7 +4942,7 @@ def try_connect(self, tls_version, expected_ssl_version):
49404942
cur = cnx.cursor()
49414943
cur.execute(query)
49424944
res = cur.fetchall()
4943-
4945+
49444946
if isinstance(expected_ssl_version, tuple):
49454947
msg = ("Not using the expected or greater TLS version: {}, instead"
49464948
" the connection used: {}.")
@@ -5553,8 +5555,26 @@ def _verify_server_name_cnx(self, use_pure=True):
55535555
cnx = mysql.connector.connect(**config)
55545556
cnx.close()
55555557

5558+
@unittest.skipIf(not CMySQLConnection, ERR_NO_CEXT)
55565559
def test_verify_server_name_cext_cnx(self):
55575560
self._verify_server_name_cnx(use_pure=False)
55585561

55595562
def test_verify_server_name_pure_cnx(self):
55605563
self._verify_server_name_cnx(use_pure=True)
5564+
5565+
5566+
@unittest.skipIf(CMySQLConnection, "Test only available without C Extension")
5567+
class BugOra27794178(tests.MySQLConnectorTests):
5568+
"""BUG#27794178: USING USE_PURE=FALSE SHOULD RAISE AN ERROR WHEN CEXT IS NOT
5569+
AVAILABLE
5570+
"""
5571+
def test_connection_use_pure(self):
5572+
config = tests.get_mysql_config().copy()
5573+
if "use_pure" in config:
5574+
del config["use_pure"]
5575+
cnx = mysql.connector.connect(**config)
5576+
cnx.close()
5577+
5578+
# Force using C Extension should fail if not available
5579+
config["use_pure"] = False
5580+
self.assertRaises(ImportError, mysql.connector.connect, **config)

tests/test_examples.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -33,11 +33,18 @@
3333

3434
from hashlib import md5
3535
import sys
36-
36+
import unittest
3737
import tests
38+
3839
from . import PY2
3940
import mysql.connector
4041

42+
try:
43+
from mysql.connector.connection_cext import CMySQLConnection
44+
except ImportError:
45+
# Test without C Extension
46+
CMySQLConnection = None
47+
4148

4249
class TestExamples(tests.MySQLConnectorTests):
4350

@@ -217,6 +224,7 @@ def test_prepared_statements(self):
217224
sys.modules.pop('examples.prepared_statements', None)
218225

219226

227+
@unittest.skipIf(not CMySQLConnection, "C Extension not available")
220228
class TestExamplesCExt(TestExamples):
221229

222230
def setUp(self):

0 commit comments

Comments
 (0)