@@ -2781,31 +2781,35 @@ def socket_factory_sequence(self):
27812781 def __init__ (self ,
27822782 connect_timeout : int = None ,
27832783 server_encoding = None ,
2784- sslmode : ('allow' , 'prefer' , 'require' , 'disable' ) = None ,
2785- sslcrtfile = None ,
2786- sslkeyfile = None ,
2787- sslrootcrtfile = None ,
2788- sslrootcrlfile = None ,
27892784 driver = None ,
27902785 ** kw
27912786 ):
27922787 super ().__init__ (** kw )
2788+ self ._security (kw )
27932789 self .driver = driver
2794-
27952790 self .server_encoding = server_encoding
27962791 self .connect_timeout = connect_timeout
2797- self .sslmode = sslmode
2798- self .sslkeyfile = sslkeyfile
2799- self .sslcrtfile = sslcrtfile
2800- self .sslrootcrtfile = sslrootcrtfile
2801- self .sslrootcrlfile = sslrootcrlfile
2792+
2793+ def _security (self , parameters ):
2794+ self .sslmode = parameters .get ('sslmode' ) or None
2795+ self .sslkeyfile = parameters .get ('sslkeyfile' ) or None
2796+ self .sslcrtfile = parameters .get ('sslcrtfile' ) or None
2797+ self .sslrootcrtfile = parameters .get ('sslrootcrtfile' ) or None
2798+ self .sslrootcrlfile = parameters .get ('sslrootcrlfile' ) or None
2799+
2800+ self ._socket_secure = {
2801+ 'keyfile' : self .sslkeyfile ,
2802+ 'certfile' : self .sslcrtfile ,
2803+ 'ca_certs' : self .sslrootcrtfile ,
2804+ }
28022805
28032806 if self .sslrootcrlfile is not None :
28042807 pg_exc .IgnoredClientParameterWarning (
28052808 "certificate revocation lists are *not* checked" ,
28062809 creator = self ,
28072810 ).emit ()
28082811
2812+ def _startup (self ):
28092813 # Startup message parameters.
28102814 tnkw = {
28112815 'client_min_messages' : 'WARNING' ,
@@ -2822,21 +2826,17 @@ def __init__(self,
28222826 )
28232827 tnkw .update (s )
28242828
2829+ # Postgres defaults the database identifier to the user.
28252830 tnkw ['user' ] = self .user
28262831 if self .database is not None :
28272832 tnkw ['database' ] = self .database
28282833
2834+ # Encode startup arguments.
2835+ # The server_encoding hint is strictly for str() values.
28292836 se = self .server_encoding or 'utf-8'
2830- ##
2831- # Attempt to accommodate for literal treatment of startup data.
2832- ##
28332837 self ._startup_parameters = tuple ([
2834- # All keys go in utf-8. However, ascii would probably be good enough.
28352838 (
28362839 k .encode ('utf-8' ),
2837- # If it's a str(), encode in the hinted server_encoding.
2838- # Otherwise, convert the object(int, float, bool, etc) into a string
2839- # and treat it as utf-8.
28402840 v .encode (se ) if type (v ) is str else str (v ).encode ('utf-8' )
28412841 )
28422842 for k , v in tnkw .items ()
@@ -2865,15 +2865,17 @@ def socket_factory_params(self, host, port, ipv, **kw):
28652865 raise TypeError ("'port' is a required keyword and cannot be 'None'" )
28662866
28672867 return {'socket_create' : (self .address_family , socket .SOCK_STREAM ),
2868- 'socket_connect' : (host , int (port ))}
2868+ 'socket_connect' : (host , int (port )),
2869+ 'socket_secure' : self ._socket_secure }
28692870
28702871 def __init__ (self , host , port , ipv , ** kw ):
2872+ super ().__init__ (** kw )
28712873 params = self .socket_factory_params (host , port , ipv , ** kw )
28722874 self .host , self .port = params ['socket_connect' ]
28732875 # constant socket connector
28742876 self ._socketcreator = self .create_socket_factory (** params )
28752877 self ._socketcreators = (self ._socketcreator ,)
2876- super (). __init__ ( ** kw )
2878+ self . _startup ( )
28772879
28782880class IP4 (IPConnector ):
28792881 """
@@ -2917,15 +2919,17 @@ def socket_factory_params(self, unix):
29172919 raise TypeError ("'unix' is a required keyword and cannot be 'None'" )
29182920
29192921 return {'socket_create' : (socket .AF_UNIX , socket .SOCK_STREAM ),
2920- 'socket_connect' : unix }
2922+ 'socket_connect' : unix ,
2923+ 'socket_secure' : self ._socket_secure }
29212924
29222925 def __init__ (self , unix = None , ** kw ):
2926+ super ().__init__ (** kw )
29232927 params = self .socket_factory_params (unix )
29242928 self .unix = params ['socket_connect' ]
29252929 # constant socket connector
29262930 self ._socketcreator = self .create_socket_factory (** params )
29272931 self ._socketcreators = (self ._socketcreator ,)
2928- super (). __init__ ( ** kw )
2932+ self . _startup ( )
29292933
29302934class Host (SocketConnector ):
29312935 """
@@ -2959,6 +2963,8 @@ def __init__(self,
29592963 address_family = None ,
29602964 ** kw
29612965 ):
2966+ super ().__init__ (** kw )
2967+
29622968 if host is None :
29632969 raise TypeError ("'host' is a required keyword" )
29642970 if port is None :
@@ -2977,7 +2983,7 @@ def __init__(self,
29772983 raise TypeError ("unknown IP version selected: 'ipv' = " + repr (ipv ))
29782984 self .host = host
29792985 self .port = port
2980- super (). __init__ ( ** kw )
2986+ self . _startup ( )
29812987
29822988class Driver (pg_api .Driver ):
29832989 def _e_metas (self ):
0 commit comments