@@ -994,71 +994,67 @@ def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
994
994
if context .check_hostname and not server_hostname :
995
995
raise ValueError ("check_hostname requires server_hostname" )
996
996
997
+ sock_timeout = sock .gettimeout ()
997
998
kwargs = dict (
998
999
family = sock .family , type = sock .type , proto = sock .proto ,
999
1000
fileno = sock .fileno ()
1000
1001
)
1001
1002
self = cls .__new__ (cls , ** kwargs )
1002
1003
super (SSLSocket , self ).__init__ (** kwargs )
1003
- sock_timeout = sock .gettimeout ()
1004
1004
sock .detach ()
1005
-
1006
- self ._context = context
1007
- self ._session = session
1008
- self ._closed = False
1009
- self ._sslobj = None
1010
- self .server_side = server_side
1011
- self .server_hostname = context ._encode_hostname (server_hostname )
1012
- self .do_handshake_on_connect = do_handshake_on_connect
1013
- self .suppress_ragged_eofs = suppress_ragged_eofs
1014
-
1015
- # See if we are connected
1005
+ # Now SSLSocket is responsible for closing the file descriptor.
1016
1006
try :
1017
- self .getpeername ()
1018
- except OSError as e :
1019
- if e .errno != errno .ENOTCONN :
1020
- raise
1021
- connected = False
1022
- blocking = self .getblocking ()
1023
- self .setblocking (False )
1007
+ self ._context = context
1008
+ self ._session = session
1009
+ self ._closed = False
1010
+ self ._sslobj = None
1011
+ self .server_side = server_side
1012
+ self .server_hostname = context ._encode_hostname (server_hostname )
1013
+ self .do_handshake_on_connect = do_handshake_on_connect
1014
+ self .suppress_ragged_eofs = suppress_ragged_eofs
1015
+
1016
+ # See if we are connected
1024
1017
try :
1025
- # We are not connected so this is not supposed to block, but
1026
- # testing revealed otherwise on macOS and Windows so we do
1027
- # the non-blocking dance regardless. Our raise when any data
1028
- # is found means consuming the data is harmless.
1029
- notconn_pre_handshake_data = self .recv (1 )
1018
+ self .getpeername ()
1030
1019
except OSError as e :
1031
- # EINVAL occurs for recv(1) on non-connected on unix sockets.
1032
- if e .errno not in (errno .ENOTCONN , errno .EINVAL ):
1020
+ if e .errno != errno .ENOTCONN :
1033
1021
raise
1034
- notconn_pre_handshake_data = b''
1035
- self .setblocking (blocking )
1036
- if notconn_pre_handshake_data :
1037
- # This prevents pending data sent to the socket before it was
1038
- # closed from escaping to the caller who could otherwise
1039
- # presume it came through a successful TLS connection.
1040
- reason = "Closed before TLS handshake with data in recv buffer."
1041
- notconn_pre_handshake_data_error = SSLError (e .errno , reason )
1042
- # Add the SSLError attributes that _ssl.c always adds.
1043
- notconn_pre_handshake_data_error .reason = reason
1044
- notconn_pre_handshake_data_error .library = None
1045
- try :
1046
- self .close ()
1047
- except OSError :
1048
- pass
1022
+ connected = False
1023
+ blocking = self .getblocking ()
1024
+ self .setblocking (False )
1049
1025
try :
1050
- raise notconn_pre_handshake_data_error
1051
- finally :
1052
- # Explicitly break the reference cycle.
1053
- notconn_pre_handshake_data_error = None
1054
- else :
1055
- connected = True
1026
+ # We are not connected so this is not supposed to block, but
1027
+ # testing revealed otherwise on macOS and Windows so we do
1028
+ # the non-blocking dance regardless. Our raise when any data
1029
+ # is found means consuming the data is harmless.
1030
+ notconn_pre_handshake_data = self .recv (1 )
1031
+ except OSError as e :
1032
+ # EINVAL occurs for recv(1) on non-connected on unix sockets.
1033
+ if e .errno not in (errno .ENOTCONN , errno .EINVAL ):
1034
+ raise
1035
+ notconn_pre_handshake_data = b''
1036
+ self .setblocking (blocking )
1037
+ if notconn_pre_handshake_data :
1038
+ # This prevents pending data sent to the socket before it was
1039
+ # closed from escaping to the caller who could otherwise
1040
+ # presume it came through a successful TLS connection.
1041
+ reason = "Closed before TLS handshake with data in recv buffer."
1042
+ notconn_pre_handshake_data_error = SSLError (e .errno , reason )
1043
+ # Add the SSLError attributes that _ssl.c always adds.
1044
+ notconn_pre_handshake_data_error .reason = reason
1045
+ notconn_pre_handshake_data_error .library = None
1046
+ try :
1047
+ raise notconn_pre_handshake_data_error
1048
+ finally :
1049
+ # Explicitly break the reference cycle.
1050
+ notconn_pre_handshake_data_error = None
1051
+ else :
1052
+ connected = True
1056
1053
1057
- self .settimeout (sock_timeout ) # Must come after setblocking() calls.
1058
- self ._connected = connected
1059
- if connected :
1060
- # create the SSL object
1061
- try :
1054
+ self .settimeout (sock_timeout ) # Must come after setblocking() calls.
1055
+ self ._connected = connected
1056
+ if connected :
1057
+ # create the SSL object
1062
1058
self ._sslobj = self ._context ._wrap_socket (
1063
1059
self , server_side , self .server_hostname ,
1064
1060
owner = self , session = self ._session ,
@@ -1069,9 +1065,12 @@ def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
1069
1065
# non-blocking
1070
1066
raise ValueError ("do_handshake_on_connect should not be specified for non-blocking sockets" )
1071
1067
self .do_handshake ()
1072
- except (OSError , ValueError ):
1068
+ except :
1069
+ try :
1073
1070
self .close ()
1074
- raise
1071
+ except OSError :
1072
+ pass
1073
+ raise
1075
1074
return self
1076
1075
1077
1076
@property
0 commit comments