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