Skip to content

Commit ffdcfd5

Browse files
committed
PYTHON-1378 - Various fixes
- Don't use _escapify - Always return str in python 3 - Further exception handling improvements
1 parent dcd8f68 commit ffdcfd5

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

pymongo/uri_parser.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@
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
25-
from dns import rdata, resolver
21+
from dns import resolver
2622
_HAVE_DNSPYTHON = True
2723
except ImportError:
2824
_HAVE_DNSPYTHON = False
@@ -271,6 +267,8 @@ def split_hosts(hosts, default_port=DEFAULT_PORT):
271267

272268

273269
if PY3:
270+
# dnspython can return bytes or str from various parts
271+
# of its API depending on version. We always want str.
274272
def maybe_decode(text):
275273
if isinstance(text, bytes):
276274
return text.decode()
@@ -283,23 +281,22 @@ def maybe_decode(text):
283281
def _get_dns_srv_hosts(hostname):
284282
try:
285283
results = resolver.query('_mongodb._tcp.' + hostname, 'SRV')
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)
289-
for res in results]
290-
except dns.exception.DNSException as exc:
284+
except Exception as exc:
291285
raise ConfigurationError(str(exc))
286+
return [(maybe_decode(res.target.to_text(omit_final_dot=True)), res.port)
287+
for res in results]
292288

293289

294290
def _get_dns_txt_options(hostname):
295291
try:
296292
results = resolver.query(hostname, 'TXT')
297-
return '&'.join(['&'.join(["%s" % (rdata._escapify(ent),)
298-
for ent in res.strings])
299-
for res in results])
300293
except resolver.NoAnswer:
301294
# No TXT records
302295
return None
296+
except Exception as exc:
297+
raise ConfigurationError(str(exc))
298+
return '&'.join([maybe_decode(val)
299+
for res in results for val in res.strings])
303300

304301

305302
def parse_uri(uri, default_port=DEFAULT_PORT, validate=True, warn=False):

0 commit comments

Comments
 (0)