@@ -276,21 +276,20 @@ def _parse_hostlist(hostlist: str,
276276 return hosts , ports
277277
278278
279- def _parse_tls_version (tls_version : str ) -> ssl_module .TLSVersion :
280- if not hasattr (ssl_module , 'TLSVersion' ):
281- raise ValueError (
282- "TLSVersion is not supported in this version of Python"
283- )
284- if tls_version .startswith ('SSL' ):
285- raise ValueError (
286- f"Unsupported TLS version: { tls_version } "
287- )
288- try :
289- return ssl_module .TLSVersion [tls_version .replace ('.' , '_' )]
290- except KeyError :
291- raise ValueError (
292- f"No such TLS version: { tls_version } "
293- )
279+ if sys .version_info >= (3 , 7 ):
280+ def _parse_tls_version (tls_version : str ) -> ssl_module .TLSVersion :
281+ if tls_version .startswith ('SSL' ):
282+ raise ValueError (
283+ f"Unsupported TLS version: { tls_version } "
284+ )
285+ try :
286+ return ssl_module .TLSVersion [
287+ tls_version .replace ('.' , '_' )
288+ ]
289+ except KeyError :
290+ raise ValueError (
291+ f"No such TLS version: { tls_version } "
292+ )
294293
295294
296295def _dot_postgresql_path (filename : str ) -> pathlib .Path :
@@ -621,30 +620,33 @@ def _parse_connect_dsn_and_args(*, dsn: typing.Optional[str],
621620 pass
622621
623622 # OpenSSL 1.1.1 keylog file, copied from create_default_context()
624- if hasattr (ssl , 'keylog_filename' ):
623+ if sys .version_info >= (3 , 8 ):
624+ # Python 3.6 and 3.7 do not have keylog_filename
625625 keylogfile = os .environ .get ('SSLKEYLOGFILE' )
626626 if keylogfile and not sys .flags .ignore_environment :
627627 ssl .keylog_filename = keylogfile
628628
629- if ssl_min_protocol_version is None :
630- ssl_min_protocol_version = os . getenv ( 'PGSSLMINPROTOCOLVERSION' )
631- if ssl_min_protocol_version :
632- ssl . minimum_version = _parse_tls_version (
633- ssl_min_protocol_version
634- )
635- else :
636- try :
637- ssl . minimum_version = _parse_tls_version ( 'TLSv1.2' )
638- except ValueError :
639- # Python 3.6 does not have ssl.TLSVersion
640- pass
629+ if sys . version_info >= ( 3 , 7 ) :
630+ # Python 3.6 does not have TLSVersion
631+ if ssl_min_protocol_version is None :
632+ ssl_min_protocol_version = os . getenv (
633+ 'PGSSLMINPROTOCOLVERSION'
634+ )
635+ if not ssl_min_protocol_version :
636+ ssl_min_protocol_version = 'TLSv1.2'
637+ if ssl_min_protocol_version :
638+ ssl . minimum_version = _parse_tls_version (
639+ ssl_min_protocol_version
640+ )
641641
642- if ssl_max_protocol_version is None :
643- ssl_max_protocol_version = os .getenv ('PGSSLMAXPROTOCOLVERSION' )
644- if ssl_max_protocol_version :
645- ssl .maximum_version = _parse_tls_version (
646- ssl_max_protocol_version
647- )
642+ if ssl_max_protocol_version is None :
643+ ssl_max_protocol_version = os .getenv (
644+ 'PGSSLMAXPROTOCOLVERSION'
645+ )
646+ if ssl_max_protocol_version :
647+ ssl .maximum_version = _parse_tls_version (
648+ ssl_max_protocol_version
649+ )
648650
649651 elif ssl is True :
650652 ssl = ssl_module .create_default_context ()
0 commit comments