Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3bc856

Browse files
committed
(Merge 3.4) Closes #21582: Cleanup test_asyncore. Patch written by diana.
- Use support.captured_stderr() where appropriate - Removes some "from test.support import xxx" import and uses support.xxx instead.
2 parents 7b2ecc4 + 252d40e commit c3bc856

1 file changed

Lines changed: 17 additions & 36 deletions

File tree

Lib/test/test_asyncore.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
import socket
66
import sys
77
import time
8-
import warnings
98
import errno
109
import struct
1110

1211
from test import support
13-
from test.support import TESTFN, run_unittest, unlink, HOST, HOSTv6
1412
from io import BytesIO
15-
from io import StringIO
1613

1714
try:
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):
388369
class 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

794775
class 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')
799780
class 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')
804785
class 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

813794
class TestAPI_UseIPv4Select(TestAPI_UseIPv4Sockets, unittest.TestCase):

0 commit comments

Comments
 (0)