1818import warnings
1919
2020try :
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
2723except ImportError :
2824 _HAVE_DNSPYTHON = False
@@ -271,6 +267,8 @@ def split_hosts(hosts, default_port=DEFAULT_PORT):
271267
272268
273269if 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):
283281def _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
294290def _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
305302def parse_uri (uri , default_port = DEFAULT_PORT , validate = True , warn = False ):
0 commit comments