Skip to content

Commit 5705b10

Browse files
committed
PYTHON-1378 - Fix DNS seedlist discovery with Eventlet
This change also adds Python 3 support for dnspython versions back to 1.13.0, the first version to support Python 3.
1 parent c84f730 commit 5705b10

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pymongo/uri_parser.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import warnings
1919

2020
try:
21+
# Eventlet monkey patches dnspython with a copy it bundles.
22+
# We have to import dns.exception to ensure we can catch the
23+
# patched DNSException.
24+
import dns.exception
2125
from dns import rdata, resolver
22-
from dns.exception import DNSException
2326
_HAVE_DNSPYTHON = True
2427
except ImportError:
2528
_HAVE_DNSPYTHON = False
@@ -267,12 +270,24 @@ def split_hosts(hosts, default_port=DEFAULT_PORT):
267270
_BAD_DB_CHARS = re.compile('[' + re.escape(r'/ "$') + ']')
268271

269272

273+
if PY3:
274+
def maybe_decode(text):
275+
if isinstance(text, bytes):
276+
return text.decode()
277+
return text
278+
else:
279+
def maybe_decode(text):
280+
return text
281+
282+
270283
def _get_dns_srv_hosts(hostname):
271284
try:
272285
results = resolver.query('_mongodb._tcp.' + hostname, 'SRV')
273-
return [(res.target.to_text(omit_final_dot=True), res.port)
286+
# Some older versions of dnspython return bytes for target.to_text.
287+
return [(maybe_decode(
288+
res.target.to_text(omit_final_dot=True)), res.port)
274289
for res in results]
275-
except DNSException as exc:
290+
except dns.exception.DNSException as exc:
276291
raise ConfigurationError(str(exc))
277292

278293

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def build_extension(self, ext):
320320
if vi[0] == 2:
321321
extras_require = {'tls': ["ipaddress"], 'srv': ["dnspython>=1.8.0,<2.0.0"]}
322322
else:
323-
extras_require = {'tls': [], 'srv': ["dnspython>=1.15.0,<2.0.0"]}
323+
extras_require = {'tls': [], 'srv': ["dnspython>=1.13.0,<2.0.0"]}
324324
if sys.platform == 'win32':
325325
extras_require['gssapi'] = ["winkerberos>=0.5.0"]
326326
if vi[0] == 2 and vi < (2, 7, 9) or vi[0] == 3 and vi < (3, 4):

0 commit comments

Comments
 (0)