Skip to content

Commit db2d293

Browse files
committed
BUG30764641: Fix dnspython dependency
In the WL#13372 implementation, the dnspython module dependency was added for host configuration using DNS SRV. This patch removes the need of having dnspython installed if no DNS SRV functionality is not being used. But raises an exception if it's used.
1 parent 9906099 commit db2d293

File tree

8 files changed

+43
-16
lines changed

8 files changed

+43
-16
lines changed

docs/mysqlx/requirements.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ Requirements
33

44
* MySQL 8.0.0 or higher, with the X Plugin enabled
55
* Python 2.7 or >= 3.4
6-
* Protobuf C++ (version >= 3.6.1)
7-
* Python Protobuf (version >= 3.6.1)
8-
* dnspython (version==1.16.0)
6+
* Protobuf C++ (version >= 3.0.0)
7+
* Python Protobuf (version >= 3.0.0)
8+
* dnspython (version >= 1.16.0)

docs/mysqlx/tutorials/getting_started.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ Resolving DNS SRV records
190190

191191
If you are using a DNS server with service discovery utility that supports mapping `SRV records <https://tools.ietf.org/html/rfc2782>`_, you can use the ``mysqlx+srv`` scheme or ``dns-srv`` connection option and Connector/Python will automatically resolve the available server addresses described by those SRV records.
192192

193+
.. note:: MySQL host configuration using DNS SRV requires `dnspython <http://www.dnspython.org/>`_ module.
194+
193195
.. code-block:: python
194196
195197
session = mysqlx.get_session('mysqlx://root:@foo.abc.com')

lib/mysql/connector/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2009, 2020, 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
@@ -38,8 +38,14 @@
3838
else:
3939
HAVE_CEXT = True
4040

41-
import dns.resolver
42-
import dns.exception
41+
try:
42+
import dns.resolver
43+
import dns.exception
44+
except ImportError:
45+
HAVE_DNSPYTHON = False
46+
else:
47+
HAVE_DNSPYTHON = True
48+
4349
import random
4450

4551
from . import version
@@ -189,6 +195,10 @@ def connect(*args, **kwargs):
189195
raise InterfaceError("The value of 'dns-srv' must be a boolean")
190196

191197
if dns_srv:
198+
if not HAVE_DNSPYTHON:
199+
raise InterfaceError('MySQL host configuration requested DNS '
200+
'SRV. This requires the Python dnspython '
201+
'module. Please refer to documentation')
192202
if 'unix_socket' in kwargs:
193203
raise InterfaceError('Using Unix domain sockets with DNS SRV '
194204
'lookup is not allowed')

lib/mysqlx/connection.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2016, 2020, 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
@@ -58,8 +58,13 @@
5858
import re
5959
import threading
6060

61-
import dns.resolver
62-
import dns.exception
61+
try:
62+
import dns.resolver
63+
import dns.exception
64+
except ImportError:
65+
HAVE_DNSPYTHON = False
66+
else:
67+
HAVE_DNSPYTHON = True
6368

6469
from datetime import datetime, timedelta
6570
from functools import wraps
@@ -1716,6 +1721,10 @@ def __init__(self, settings):
17161721

17171722
# Check for DNS SRV
17181723
if settings.get("host") and settings.get("dns-srv"):
1724+
if not HAVE_DNSPYTHON:
1725+
raise InterfaceError("MySQL host configuration requested DNS "
1726+
"SRV. This requires the Python dnspython "
1727+
"module. Please refer to documentation")
17191728
try:
17201729
srv_records = dns.resolver.query(settings["host"], "SRV")
17211730
except dns.exception.DNSException:

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
# Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
4+
# Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License, version 2.0, as
@@ -69,5 +69,6 @@
6969
cmdclass=setupinfo.command_classes,
7070
ext_modules=setupinfo.extensions,
7171
install_requires=setupinfo.install_requires,
72+
extras_require=setupinfo.extras_require,
7273
)
7374

setupinfo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,5 @@
139139
'Topic :: Software Development :: Libraries :: Application Frameworks',
140140
'Topic :: Software Development :: Libraries :: Python Modules'
141141
]
142-
install_requires = ["protobuf>=3.6.1", "dnspython==1.16.0"]
142+
install_requires = ["protobuf>=3.0.0"],
143+
extras_require = {"dns-srv": ["dnspython>=1.16.0"]}

tests/test_connection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2009, 2020, 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
@@ -53,7 +53,8 @@
5353

5454
from mysql.connector.conversion import (MySQLConverterBase, MySQLConverter)
5555
from mysql.connector import (connect, connection, network, errors,
56-
constants, cursor, abstracts, catch23)
56+
constants, cursor, abstracts, catch23,
57+
HAVE_DNSPYTHON)
5758
from mysql.connector.errors import InterfaceError
5859
from mysql.connector.optionfiles import read_option_files
5960
from mysql.connector.network import TLS_V1_3_SUPPORTED
@@ -2155,6 +2156,8 @@ def test_connection_attributes_user_defined(self):
21552156

21562157
@unittest.skipIf(tests.MYSQL_VERSION < (8, 0, 19),
21572158
"MySQL 8.0.19+ is required for DNS SRV")
2159+
@unittest.skipIf(not HAVE_DNSPYTHON,
2160+
"dnspython module is required for DNS SRV")
21582161
def test_dns_srv(self):
21592162
config = tests.get_mysql_config().copy()
21602163
config.pop("unix_socket", None)

tests/test_mysqlx_connection.py

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

3-
# Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2016, 2020, 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
@@ -46,8 +46,7 @@
4646
from time import sleep
4747

4848
from . import check_tls_versions_support
49-
from mysqlx.connection import SocketStream
50-
from mysqlx.connection import TLS_V1_3_SUPPORTED
49+
from mysqlx.connection import SocketStream, TLS_V1_3_SUPPORTED, HAVE_DNSPYTHON
5150
from mysqlx.compat import STRING_TYPES
5251
from mysqlx.errors import InterfaceError, OperationalError, ProgrammingError
5352
from mysqlx.protocol import Message, MessageReaderWriter, Protocol
@@ -1532,6 +1531,8 @@ def test_connection_attributes(self):
15321531

15331532
@unittest.skipIf(tests.MYSQL_VERSION < (8, 0, 19),
15341533
"MySQL 8.0.19+ is required for DNS SRV")
1534+
@unittest.skipIf(not HAVE_DNSPYTHON,
1535+
"dnspython module is required for DNS SRV")
15351536
def test_dns_srv(self):
15361537
# The value of 'dns-srv' must be a boolean
15371538
uri = "root:@localhost/myschema?dns-srv=invalid"

0 commit comments

Comments
 (0)