@@ -1031,71 +1031,67 @@ def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
1031
1031
if context .check_hostname and not server_hostname :
1032
1032
raise ValueError ("check_hostname requires server_hostname" )
1033
1033
1034
+ sock_timeout = sock .gettimeout ()
1034
1035
kwargs = dict (
1035
1036
family = sock .family , type = sock .type , proto = sock .proto ,
1036
1037
fileno = sock .fileno ()
1037
1038
)
1038
1039
self = cls .__new__ (cls , ** kwargs )
1039
1040
super (SSLSocket , self ).__init__ (** kwargs )
1040
- sock_timeout = sock .gettimeout ()
1041
1041
sock .detach ()
1042
-
1043
- self ._context = context
1044
- self ._session = session
1045
- self ._closed = False
1046
- self ._sslobj = None
1047
- self .server_side = server_side
1048
- self .server_hostname = context ._encode_hostname (server_hostname )
1049
- self .do_handshake_on_connect = do_handshake_on_connect
1050
- self .suppress_ragged_eofs = suppress_ragged_eofs
1051
-
1052
- # See if we are connected
1042
+ # Now SSLSocket is responsible for closing the file descriptor.
1053
1043
try :
1054
- self .getpeername ()
1055
- except OSError as e :
1056
- if e .errno != errno .ENOTCONN :
1057
- raise
1058
- connected = False
1059
- blocking = self .getblocking ()
1060
- self .setblocking (False )
1044
+ self ._context = context
1045
+ self ._session = session
1046
+ self ._closed = False
1047
+ self ._sslobj = None
1048
+ self .server_side = server_side
1049
+ self .server_hostname = context ._encode_hostname (server_hostname )
1050
+ self .do_handshake_on_connect = do_handshake_on_connect
1051
+ self .suppress_ragged_eofs = suppress_ragged_eofs
1052
+
1053
+ # See if we are connected
1061
1054
try :
1062
- # We are not connected so this is not supposed to block, but
1063
- # testing revealed otherwise on macOS and Windows so we do
1064
- # the non-blocking dance regardless. Our raise when any data
1065
- # is found means consuming the data is harmless.
1066
- notconn_pre_handshake_data = self .recv (1 )
1055
+ self .getpeername ()
1067
1056
except OSError as e :
1068
- # EINVAL occurs for recv(1) on non-connected on unix sockets.
1069
- if e .errno not in (errno .ENOTCONN , errno .EINVAL ):
1057
+ if e .errno != errno .ENOTCONN :
1070
1058
raise
1071
- notconn_pre_handshake_data = b''
1072
- self .setblocking (blocking )
1073
- if notconn_pre_handshake_data :
1074
- # This prevents pending data sent to the socket before it was
1075
- # closed from escaping to the caller who could otherwise
1076
- # presume it came through a successful TLS connection.
1077
- reason = "Closed before TLS handshake with data in recv buffer."
1078
- notconn_pre_handshake_data_error = SSLError (e .errno , reason )
1079
- # Add the SSLError attributes that _ssl.c always adds.
1080
- notconn_pre_handshake_data_error .reason = reason
1081
- notconn_pre_handshake_data_error .library = None
1082
- try :
1083
- self .close ()
1084
- except OSError :
1085
- pass
1059
+ connected = False
1060
+ blocking = self .getblocking ()
1061
+ self .setblocking (False )
1086
1062
try :
1087
- raise notconn_pre_handshake_data_error
1088
- finally :
1089
- # Explicitly break the reference cycle.
1090
- notconn_pre_handshake_data_error = None
1091
- else :
1092
- connected = True
1063
+ # We are not connected so this is not supposed to block, but
1064
+ # testing revealed otherwise on macOS and Windows so we do
1065
+ # the non-blocking dance regardless. Our raise when any data
1066
+ # is found means consuming the data is harmless.
1067
+ notconn_pre_handshake_data = self .recv (1 )
1068
+ except OSError as e :
1069
+ # EINVAL occurs for recv(1) on non-connected on unix sockets.
1070
+ if e .errno not in (errno .ENOTCONN , errno .EINVAL ):
1071
+ raise
1072
+ notconn_pre_handshake_data = b''
1073
+ self .setblocking (blocking )
1074
+ if notconn_pre_handshake_data :
1075
+ # This prevents pending data sent to the socket before it was
1076
+ # closed from escaping to the caller who could otherwise
1077
+ # presume it came through a successful TLS connection.
1078
+ reason = "Closed before TLS handshake with data in recv buffer."
1079
+ notconn_pre_handshake_data_error = SSLError (e .errno , reason )
1080
+ # Add the SSLError attributes that _ssl.c always adds.
1081
+ notconn_pre_handshake_data_error .reason = reason
1082
+ notconn_pre_handshake_data_error .library = None
1083
+ try :
1084
+ raise notconn_pre_handshake_data_error
1085
+ finally :
1086
+ # Explicitly break the reference cycle.
1087
+ notconn_pre_handshake_data_error = None
1088
+ else :
1089
+ connected = True
1093
1090
1094
- self .settimeout (sock_timeout ) # Must come after setblocking() calls.
1095
- self ._connected = connected
1096
- if connected :
1097
- # create the SSL object
1098
- try :
1091
+ self .settimeout (sock_timeout ) # Must come after setblocking() calls.
1092
+ self ._connected = connected
1093
+ if connected :
1094
+ # create the SSL object
1099
1095
self ._sslobj = self ._context ._wrap_socket (
1100
1096
self , server_side , self .server_hostname ,
1101
1097
owner = self , session = self ._session ,
@@ -1106,9 +1102,12 @@ def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
1106
1102
# non-blocking
1107
1103
raise ValueError ("do_handshake_on_connect should not be specified for non-blocking sockets" )
1108
1104
self .do_handshake ()
1109
- except (OSError , ValueError ):
1105
+ except :
1106
+ try :
1110
1107
self .close ()
1111
- raise
1108
+ except OSError :
1109
+ pass
1110
+ raise
1112
1111
return self
1113
1112
1114
1113
@property
0 commit comments