@@ -217,29 +217,30 @@ def test_refcycle(self):
217
217
s = socket .socket (socket .AF_INET )
218
218
ss = ssl .wrap_socket (s )
219
219
wr = weakref .ref (ss )
220
- del ss
221
- self .assertEqual (wr (), None )
220
+ with support .check_warnings (("" , ResourceWarning )):
221
+ del ss
222
+ self .assertEqual (wr (), None )
222
223
223
224
def test_wrapped_unconnected (self ):
224
225
# Methods on an unconnected SSLSocket propagate the original
225
226
# OSError raise by the underlying socket object.
226
227
s = socket .socket (socket .AF_INET )
227
- ss = ssl .wrap_socket (s )
228
- self .assertRaises (OSError , ss .recv , 1 )
229
- self .assertRaises (OSError , ss .recv_into , bytearray (b'x' ))
230
- self .assertRaises (OSError , ss .recvfrom , 1 )
231
- self .assertRaises (OSError , ss .recvfrom_into , bytearray (b'x' ), 1 )
232
- self .assertRaises (OSError , ss .send , b'x' )
233
- self .assertRaises (OSError , ss .sendto , b'x' , ('0.0.0.0' , 0 ))
228
+ with ssl .wrap_socket (s ) as ss :
229
+ self .assertRaises (OSError , ss .recv , 1 )
230
+ self .assertRaises (OSError , ss .recv_into , bytearray (b'x' ))
231
+ self .assertRaises (OSError , ss .recvfrom , 1 )
232
+ self .assertRaises (OSError , ss .recvfrom_into , bytearray (b'x' ), 1 )
233
+ self .assertRaises (OSError , ss .send , b'x' )
234
+ self .assertRaises (OSError , ss .sendto , b'x' , ('0.0.0.0' , 0 ))
234
235
235
236
def test_timeout (self ):
236
237
# Issue #8524: when creating an SSL socket, the timeout of the
237
238
# original socket should be retained.
238
239
for timeout in (None , 0.0 , 5.0 ):
239
240
s = socket .socket (socket .AF_INET )
240
241
s .settimeout (timeout )
241
- ss = ssl .wrap_socket (s )
242
- self .assertEqual (timeout , ss .gettimeout ())
242
+ with ssl .wrap_socket (s ) as ss :
243
+ self .assertEqual (timeout , ss .gettimeout ())
243
244
244
245
def test_errors (self ):
245
246
sock = socket .socket ()
@@ -252,9 +253,9 @@ def test_errors(self):
252
253
self .assertRaisesRegex (ValueError ,
253
254
"certfile must be specified for server-side operations" ,
254
255
ssl .wrap_socket , sock , server_side = True , certfile = "" )
255
- s = ssl .wrap_socket (sock , server_side = True , certfile = CERTFILE )
256
- self .assertRaisesRegex (ValueError , "can't connect in server-side mode" ,
257
- s .connect , (HOST , 8080 ))
256
+ with ssl .wrap_socket (sock , server_side = True , certfile = CERTFILE ) as s :
257
+ self .assertRaisesRegex (ValueError , "can't connect in server-side mode" ,
258
+ s .connect , (HOST , 8080 ))
258
259
with self .assertRaises (OSError ) as cm :
259
260
with socket .socket () as sock :
260
261
ssl .wrap_socket (sock , certfile = WRONGCERT )
@@ -367,21 +368,21 @@ def test_server_side(self):
367
368
def test_unknown_channel_binding (self ):
368
369
# should raise ValueError for unknown type
369
370
s = socket .socket (socket .AF_INET )
370
- ss = ssl .wrap_socket (s )
371
- with self .assertRaises (ValueError ):
372
- ss .get_channel_binding ("unknown-type" )
371
+ with ssl .wrap_socket (s ) as ss :
372
+ with self .assertRaises (ValueError ):
373
+ ss .get_channel_binding ("unknown-type" )
373
374
374
375
@unittest .skipUnless ("tls-unique" in ssl .CHANNEL_BINDING_TYPES ,
375
376
"'tls-unique' channel binding not available" )
376
377
def test_tls_unique_channel_binding (self ):
377
378
# unconnected should return None for known type
378
379
s = socket .socket (socket .AF_INET )
379
- ss = ssl .wrap_socket (s )
380
- self .assertIsNone (ss .get_channel_binding ("tls-unique" ))
380
+ with ssl .wrap_socket (s ) as ss :
381
+ self .assertIsNone (ss .get_channel_binding ("tls-unique" ))
381
382
# the same for server-side
382
383
s = socket .socket (socket .AF_INET )
383
- ss = ssl .wrap_socket (s , server_side = True , certfile = CERTFILE )
384
- self .assertIsNone (ss .get_channel_binding ("tls-unique" ))
384
+ with ssl .wrap_socket (s , server_side = True , certfile = CERTFILE ) as ss :
385
+ self .assertIsNone (ss .get_channel_binding ("tls-unique" ))
385
386
386
387
def test_dealloc_warn (self ):
387
388
ss = ssl .wrap_socket (socket .socket (socket .AF_INET ))
@@ -660,10 +661,10 @@ def test_subclass(self):
660
661
with socket .socket () as s :
661
662
s .bind (("127.0.0.1" , 0 ))
662
663
s .listen (5 )
663
- with socket .socket () as c :
664
- c .connect (s .getsockname ())
665
- c .setblocking (False )
666
- c = ctx .wrap_socket (c , False , do_handshake_on_connect = False )
664
+ c = socket .socket ()
665
+ c .connect (s .getsockname ())
666
+ c .setblocking (False )
667
+ with ctx .wrap_socket (c , False , do_handshake_on_connect = False ) as c :
667
668
with self .assertRaises (ssl .SSLWantReadError ) as cm :
668
669
c .do_handshake ()
669
670
s = str (cm .exception )
@@ -904,12 +905,12 @@ def _test_get_server_certificate(host, port, cert=None):
904
905
def test_ciphers (self ):
905
906
remote = ("svn.python.org" , 443 )
906
907
with support .transient_internet (remote [0 ]):
907
- s = ssl .wrap_socket (socket .socket (socket .AF_INET ),
908
- cert_reqs = ssl .CERT_NONE , ciphers = "ALL" )
909
- s .connect (remote )
910
- s = ssl .wrap_socket (socket .socket (socket .AF_INET ),
911
- cert_reqs = ssl .CERT_NONE , ciphers = "DEFAULT" )
912
- s .connect (remote )
908
+ with ssl .wrap_socket (socket .socket (socket .AF_INET ),
909
+ cert_reqs = ssl .CERT_NONE , ciphers = "ALL" ) as s :
910
+ s .connect (remote )
911
+ with ssl .wrap_socket (socket .socket (socket .AF_INET ),
912
+ cert_reqs = ssl .CERT_NONE , ciphers = "DEFAULT" ) as s :
913
+ s .connect (remote )
913
914
# Error checking can happen at instantiation or when connecting
914
915
with self .assertRaisesRegex (ssl .SSLError , "No cipher can be selected" ):
915
916
with socket .socket (socket .AF_INET ) as sock :
@@ -1886,6 +1887,8 @@ def serve():
1886
1887
client_addr = client .getsockname ()
1887
1888
client .close ()
1888
1889
t .join ()
1890
+ remote .close ()
1891
+ server .close ()
1889
1892
# Sanity checks.
1890
1893
self .assertIsInstance (remote , ssl .SSLSocket )
1891
1894
self .assertEqual (peer , client_addr )
@@ -1900,8 +1903,7 @@ def test_default_ciphers(self):
1900
1903
with ThreadedEchoServer (CERTFILE ,
1901
1904
ssl_version = ssl .PROTOCOL_SSLv23 ,
1902
1905
chatty = False ) as server :
1903
- with socket .socket () as sock :
1904
- s = context .wrap_socket (sock )
1906
+ with context .wrap_socket (socket .socket ()) as s :
1905
1907
with self .assertRaises (OSError ):
1906
1908
s .connect ((HOST , server .port ))
1907
1909
self .assertIn ("no shared cipher" , str (server .conn_errors [0 ]))
0 commit comments