55import socket
66import sys
77import time
8- import warnings
98import errno
109import struct
1110
1211from test import support
13- from test .support import TESTFN , run_unittest , unlink , HOST , HOSTv6
1412from io import BytesIO
15- from io import StringIO
1613
1714try :
1815 import threading
@@ -94,7 +91,7 @@ def bind_af_aware(sock, addr):
9491 """Helper function to bind a socket according to its family."""
9592 if HAS_UNIX_SOCKETS and sock .family == socket .AF_UNIX :
9693 # Make sure the path doesn't exist.
97- unlink (addr )
94+ support . unlink (addr )
9895 sock .bind (addr )
9996
10097
@@ -257,59 +254,43 @@ def test_log(self):
257254 d = asyncore .dispatcher ()
258255
259256 # capture output of dispatcher.log() (to stderr)
260- fp = StringIO ()
261- stderr = sys .stderr
262257 l1 = "Lovely spam! Wonderful spam!"
263258 l2 = "I don't like spam!"
264- try :
265- sys .stderr = fp
259+ with support .captured_stderr () as stderr :
266260 d .log (l1 )
267261 d .log (l2 )
268- finally :
269- sys .stderr = stderr
270262
271- lines = fp .getvalue ().splitlines ()
263+ lines = stderr .getvalue ().splitlines ()
272264 self .assertEqual (lines , ['log: %s' % l1 , 'log: %s' % l2 ])
273265
274266 def test_log_info (self ):
275267 d = asyncore .dispatcher ()
276268
277269 # capture output of dispatcher.log_info() (to stdout via print)
278- fp = StringIO ()
279- stdout = sys .stdout
280270 l1 = "Have you got anything without spam?"
281271 l2 = "Why can't she have egg bacon spam and sausage?"
282272 l3 = "THAT'S got spam in it!"
283- try :
284- sys .stdout = fp
273+ with support .captured_stdout () as stdout :
285274 d .log_info (l1 , 'EGGS' )
286275 d .log_info (l2 )
287276 d .log_info (l3 , 'SPAM' )
288- finally :
289- sys .stdout = stdout
290277
291- lines = fp .getvalue ().splitlines ()
278+ lines = stdout .getvalue ().splitlines ()
292279 expected = ['EGGS: %s' % l1 , 'info: %s' % l2 , 'SPAM: %s' % l3 ]
293-
294280 self .assertEqual (lines , expected )
295281
296282 def test_unhandled (self ):
297283 d = asyncore .dispatcher ()
298284 d .ignore_log_types = ()
299285
300286 # capture output of dispatcher.log_info() (to stdout via print)
301- fp = StringIO ()
302- stdout = sys .stdout
303- try :
304- sys .stdout = fp
287+ with support .captured_stdout () as stdout :
305288 d .handle_expt ()
306289 d .handle_read ()
307290 d .handle_write ()
308291 d .handle_connect ()
309- finally :
310- sys .stdout = stdout
311292
312- lines = fp .getvalue ().splitlines ()
293+ lines = stdout .getvalue ().splitlines ()
313294 expected = ['warning: unhandled incoming priority event' ,
314295 'warning: unhandled read event' ,
315296 'warning: unhandled write event' ,
@@ -360,7 +341,7 @@ def test_send(self):
360341 data = b"Suppose there isn't a 16-ton weight?"
361342 d = dispatcherwithsend_noread ()
362343 d .create_socket ()
363- d .connect ((HOST , port ))
344+ d .connect ((support . HOST , port ))
364345
365346 # give time for socket to connect
366347 time .sleep (0.1 )
@@ -388,14 +369,14 @@ def test_send(self):
388369class FileWrapperTest (unittest .TestCase ):
389370 def setUp (self ):
390371 self .d = b"It's not dead, it's sleeping!"
391- with open (TESTFN , 'wb' ) as file :
372+ with open (support . TESTFN , 'wb' ) as file :
392373 file .write (self .d )
393374
394375 def tearDown (self ):
395- unlink (TESTFN )
376+ support . unlink (support . TESTFN )
396377
397378 def test_recv (self ):
398- fd = os .open (TESTFN , os .O_RDONLY )
379+ fd = os .open (support . TESTFN , os .O_RDONLY )
399380 w = asyncore .file_wrapper (fd )
400381 os .close (fd )
401382
@@ -409,20 +390,20 @@ def test_recv(self):
409390 def test_send (self ):
410391 d1 = b"Come again?"
411392 d2 = b"I want to buy some cheese."
412- fd = os .open (TESTFN , os .O_WRONLY | os .O_APPEND )
393+ fd = os .open (support . TESTFN , os .O_WRONLY | os .O_APPEND )
413394 w = asyncore .file_wrapper (fd )
414395 os .close (fd )
415396
416397 w .write (d1 )
417398 w .send (d2 )
418399 w .close ()
419- with open (TESTFN , 'rb' ) as file :
400+ with open (support . TESTFN , 'rb' ) as file :
420401 self .assertEqual (file .read (), self .d + d1 + d2 )
421402
422403 @unittest .skipUnless (hasattr (asyncore , 'file_dispatcher' ),
423404 'asyncore.file_dispatcher required' )
424405 def test_dispatcher (self ):
425- fd = os .open (TESTFN , os .O_RDONLY )
406+ fd = os .open (support . TESTFN , os .O_RDONLY )
426407 data = []
427408 class FileDispatcher (asyncore .file_dispatcher ):
428409 def handle_read (self ):
@@ -793,12 +774,12 @@ def cleanup():
793774
794775class TestAPI_UseIPv4Sockets (BaseTestAPI ):
795776 family = socket .AF_INET
796- addr = (HOST , 0 )
777+ addr = (support . HOST , 0 )
797778
798779@unittest .skipUnless (support .IPV6_ENABLED , 'IPv6 support required' )
799780class TestAPI_UseIPv6Sockets (BaseTestAPI ):
800781 family = socket .AF_INET6
801- addr = (HOSTv6 , 0 )
782+ addr = (support . HOSTv6 , 0 )
802783
803784@unittest .skipUnless (HAS_UNIX_SOCKETS , 'Unix sockets required' )
804785class TestAPI_UseUnixSockets (BaseTestAPI ):
@@ -807,7 +788,7 @@ class TestAPI_UseUnixSockets(BaseTestAPI):
807788 addr = support .TESTFN
808789
809790 def tearDown (self ):
810- unlink (self .addr )
791+ support . unlink (self .addr )
811792 BaseTestAPI .tearDown (self )
812793
813794class TestAPI_UseIPv4Select (TestAPI_UseIPv4Sockets , unittest .TestCase ):
0 commit comments