Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(158416)

Delta Between Two Patch Sets: Lib/test/test_socket.py

Issue 23001: Accept mutable bytes-like objects
Left Patch Set: Created 5 years ago
Right Patch Set: Created 4 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 import unittest 1 import unittest
2 from test import support 2 from test import support
3 3
4 import errno 4 import errno
5 import io 5 import io
6 import itertools 6 import itertools
7 import socket 7 import socket
8 import select 8 import select
9 import tempfile 9 import tempfile
10 import time 10 import time
(...skipping 1362 matching lines...) Expand 10 before | Expand all | Expand 10 after
1373 fp.close() 1373 fp.close()
1374 self.assertRaises(ValueError, fp.readable) 1374 self.assertRaises(ValueError, fp.readable)
1375 self.assertRaises(ValueError, fp.writable) 1375 self.assertRaises(ValueError, fp.writable)
1376 self.assertRaises(ValueError, fp.seekable) 1376 self.assertRaises(ValueError, fp.seekable)
1377 1377
1378 def test_pickle(self): 1378 def test_pickle(self):
1379 sock = socket.socket() 1379 sock = socket.socket()
1380 with sock: 1380 with sock:
1381 for protocol in range(pickle.HIGHEST_PROTOCOL + 1): 1381 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1382 self.assertRaises(TypeError, pickle.dumps, sock, protocol) 1382 self.assertRaises(TypeError, pickle.dumps, sock, protocol)
1383 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1384 family = pickle.loads(pickle.dumps(socket.AF_INET, protocol))
1385 self.assertEqual(family, socket.AF_INET)
1386 type = pickle.loads(pickle.dumps(socket.SOCK_STREAM, protocol))
1387 self.assertEqual(type, socket.SOCK_STREAM)
1383 1388
1384 def test_listen_backlog(self): 1389 def test_listen_backlog(self):
1385 for backlog in 0, -1: 1390 for backlog in 0, -1:
1386 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv: 1391 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
1387 srv.bind((HOST, 0)) 1392 srv.bind((HOST, 0))
1388 srv.listen(backlog) 1393 srv.listen(backlog)
1389 1394
1390 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv: 1395 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
1391 srv.bind((HOST, 0)) 1396 srv.bind((HOST, 0))
1392 srv.listen() 1397 srv.listen()
(...skipping 2194 matching lines...) Expand 10 before | Expand all | Expand 10 after
3587 # signal to the wrong one. 3592 # signal to the wrong one.
3588 3593
3589 class InterruptedTimeoutBase(unittest.TestCase): 3594 class InterruptedTimeoutBase(unittest.TestCase):
3590 # Base class for interrupted send/receive tests. Installs an 3595 # Base class for interrupted send/receive tests. Installs an
3591 # empty handler for SIGALRM and removes it on teardown, along with 3596 # empty handler for SIGALRM and removes it on teardown, along with
3592 # any scheduled alarms. 3597 # any scheduled alarms.
3593 3598
3594 def setUp(self): 3599 def setUp(self):
3595 super().setUp() 3600 super().setUp()
3596 orig_alrm_handler = signal.signal(signal.SIGALRM, 3601 orig_alrm_handler = signal.signal(signal.SIGALRM,
3597 lambda signum, frame: None) 3602 lambda signum, frame: 1 / 0)
3598 self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) 3603 self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler)
3599 self.addCleanup(self.setAlarm, 0) 3604 self.addCleanup(self.setAlarm, 0)
3600 3605
3601 # Timeout for socket operations 3606 # Timeout for socket operations
3602 timeout = 4.0 3607 timeout = 4.0
3603 3608
3604 # Provide setAlarm() method to schedule delivery of SIGALRM after 3609 # Provide setAlarm() method to schedule delivery of SIGALRM after
3605 # given number of seconds, or cancel it if zero, and an 3610 # given number of seconds, or cancel it if zero, and an
3606 # appropriate time value to use. Use setitimer() if available. 3611 # appropriate time value to use. Use setitimer() if available.
3607 if hasattr(signal, "setitimer"): 3612 if hasattr(signal, "setitimer"):
(...skipping 16 matching lines...) Expand all
3624 "Don't have signal.alarm or signal.setitimer") 3629 "Don't have signal.alarm or signal.setitimer")
3625 class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase): 3630 class InterruptedRecvTimeoutTest(InterruptedTimeoutBase, UDPTestBase):
3626 # Test interrupting the recv*() methods with signals when a 3631 # Test interrupting the recv*() methods with signals when a
3627 # timeout is set. 3632 # timeout is set.
3628 3633
3629 def setUp(self): 3634 def setUp(self):
3630 super().setUp() 3635 super().setUp()
3631 self.serv.settimeout(self.timeout) 3636 self.serv.settimeout(self.timeout)
3632 3637
3633 def checkInterruptedRecv(self, func, *args, **kwargs): 3638 def checkInterruptedRecv(self, func, *args, **kwargs):
3634 # Check that func(*args, **kwargs) raises OSError with an 3639 # Check that func(*args, **kwargs) raises
3635 # errno of EINTR when interrupted by a signal. 3640 # errno of EINTR when interrupted by a signal.
3636 self.setAlarm(self.alarm_time) 3641 self.setAlarm(self.alarm_time)
3637 with self.assertRaises(OSError) as cm: 3642 with self.assertRaises(ZeroDivisionError) as cm:
3638 func(*args, **kwargs) 3643 func(*args, **kwargs)
3639 self.assertNotIsInstance(cm.exception, socket.timeout)
3640 self.assertEqual(cm.exception.errno, errno.EINTR)
3641 3644
3642 def testInterruptedRecvTimeout(self): 3645 def testInterruptedRecvTimeout(self):
3643 self.checkInterruptedRecv(self.serv.recv, 1024) 3646 self.checkInterruptedRecv(self.serv.recv, 1024)
3644 3647
3645 def testInterruptedRecvIntoTimeout(self): 3648 def testInterruptedRecvIntoTimeout(self):
3646 self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024)) 3649 self.checkInterruptedRecv(self.serv.recv_into, bytearray(1024))
3647 3650
3648 def testInterruptedRecvfromTimeout(self): 3651 def testInterruptedRecvfromTimeout(self):
3649 self.checkInterruptedRecv(self.serv.recvfrom, 1024) 3652 self.checkInterruptedRecv(self.serv.recvfrom, 1024)
3650 3653
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3686 cli_thread.join() 3689 cli_thread.join()
3687 self.serv_conn.settimeout(self.timeout) 3690 self.serv_conn.settimeout(self.timeout)
3688 3691
3689 def doConnect(self): 3692 def doConnect(self):
3690 self.serv_conn.connect(self.serv_addr) 3693 self.serv_conn.connect(self.serv_addr)
3691 3694
3692 def checkInterruptedSend(self, func, *args, **kwargs): 3695 def checkInterruptedSend(self, func, *args, **kwargs):
3693 # Check that func(*args, **kwargs), run in a loop, raises 3696 # Check that func(*args, **kwargs), run in a loop, raises
3694 # OSError with an errno of EINTR when interrupted by a 3697 # OSError with an errno of EINTR when interrupted by a
3695 # signal. 3698 # signal.
3696 with self.assertRaises(OSError) as cm: 3699 with self.assertRaises(ZeroDivisionError) as cm:
3697 while True: 3700 while True:
3698 self.setAlarm(self.alarm_time) 3701 self.setAlarm(self.alarm_time)
3699 func(*args, **kwargs) 3702 func(*args, **kwargs)
3700 self.assertNotIsInstance(cm.exception, socket.timeout)
3701 self.assertEqual(cm.exception.errno, errno.EINTR)
3702 3703
3703 # Issue #12958: The following tests have problems on OS X prior to 10.7 3704 # Issue #12958: The following tests have problems on OS X prior to 10.7
3704 @support.requires_mac_ver(10, 7) 3705 @support.requires_mac_ver(10, 7)
3705 def testInterruptedSendTimeout(self): 3706 def testInterruptedSendTimeout(self):
3706 self.checkInterruptedSend(self.serv_conn.send, b"a"*512) 3707 self.checkInterruptedSend(self.serv_conn.send, b"a"*512)
3707 3708
3708 @support.requires_mac_ver(10, 7) 3709 @support.requires_mac_ver(10, 7)
3709 def testInterruptedSendtoTimeout(self): 3710 def testInterruptedSendtoTimeout(self):
3710 # Passing an actual address here as Python's wrapper for 3711 # Passing an actual address here as Python's wrapper for
3711 # sendto() doesn't allow passing a zero-length one; POSIX 3712 # sendto() doesn't allow passing a zero-length one; POSIX
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
4057 self.assertEqual(self.write_file.name, self.serv_conn.fileno()) 4058 self.assertEqual(self.write_file.name, self.serv_conn.fileno())
4058 4059
4059 def testRealClose(self): 4060 def testRealClose(self):
4060 self.read_file.close() 4061 self.read_file.close()
4061 self.assertRaises(ValueError, self.read_file.fileno) 4062 self.assertRaises(ValueError, self.read_file.fileno)
4062 self.cli_conn.close() 4063 self.cli_conn.close()
4063 self.assertRaises(OSError, self.cli_conn.getsockname) 4064 self.assertRaises(OSError, self.cli_conn.getsockname)
4064 4065
4065 def _testRealClose(self): 4066 def _testRealClose(self):
4066 pass 4067 pass
4067
4068
4069 class FileObjectInterruptedTestCase(unittest.TestCase):
4070 """Test that the file object correctly handles EINTR internally."""
4071
4072 class MockSocket(object):
4073 def __init__(self, recv_funcs=()):
4074 # A generator that returns callables that we'll call for each
4075 # call to recv().
4076 self._recv_step = iter(recv_funcs)
4077
4078 def recv_into(self, buffer):
4079 data = next(self._recv_step)()
4080 assert len(buffer) >= len(data)
4081 buffer[:len(data)] = data
4082 return len(data)
4083
4084 def _decref_socketios(self):
4085 pass
4086
4087 def _textiowrap_for_test(self, buffering=-1):
4088 raw = socket.SocketIO(self, "r")
4089 if buffering < 0:
4090 buffering = io.DEFAULT_BUFFER_SIZE
4091 if buffering == 0:
4092 return raw
4093 buffer = io.BufferedReader(raw, buffering)
4094 text = io.TextIOWrapper(buffer, None, None)
4095 text.mode = "rb"
4096 return text
4097
4098 @staticmethod
4099 def _raise_eintr():
4100 raise OSError(errno.EINTR, "interrupted")
4101
4102 def _textiowrap_mock_socket(self, mock, buffering=-1):
4103 raw = socket.SocketIO(mock, "r")
4104 if buffering < 0:
4105 buffering = io.DEFAULT_BUFFER_SIZE
4106 if buffering == 0:
4107 return raw
4108 buffer = io.BufferedReader(raw, buffering)
4109 text = io.TextIOWrapper(buffer, None, None)
4110 text.mode = "rb"
4111 return text
4112
4113 def _test_readline(self, size=-1, buffering=-1):
4114 mock_sock = self.MockSocket(recv_funcs=[
4115 lambda : b"This is the first line\nAnd the sec",
4116 self._raise_eintr,
4117 lambda : b"ond line is here\n",
4118 lambda : b"",
4119 lambda : b"", # XXX(gps): io library does an extra EOF read
4120 ])
4121 fo = mock_sock._textiowrap_for_test(buffering=buffering)
4122 self.assertEqual(fo.readline(size), "This is the first line\n")
4123 self.assertEqual(fo.readline(size), "And the second line is here\n")
4124
4125 def _test_read(self, size=-1, buffering=-1):
4126 mock_sock = self.MockSocket(recv_funcs=[
4127 lambda : b"This is the first line\nAnd the sec",
4128 self._raise_eintr,
4129 lambda : b"ond line is here\n",
4130 lambda : b"",
4131 lambda : b"", # XXX(gps): io library does an extra EOF read
4132 ])
4133 expecting = (b"This is the first line\n"
4134 b"And the second line is here\n")
4135 fo = mock_sock._textiowrap_for_test(buffering=buffering)
4136 if buffering == 0:
4137 data = b''
4138 else:
4139 data = ''
4140 expecting = expecting.decode('utf-8')
4141 while len(data) != len(expecting):
4142 part = fo.read(size)
4143 if not part:
4144 break
4145 data += part
4146 self.assertEqual(data, expecting)
4147
4148 def test_default(self):
4149 self._test_readline()
4150 self._test_readline(size=100)
4151 self._test_read()
4152 self._test_read(size=100)
4153
4154 def test_with_1k_buffer(self):
4155 self._test_readline(buffering=1024)
4156 self._test_readline(size=100, buffering=1024)
4157 self._test_read(buffering=1024)
4158 self._test_read(size=100, buffering=1024)
4159
4160 def _test_readline_no_buffer(self, size=-1):
4161 mock_sock = self.MockSocket(recv_funcs=[
4162 lambda : b"a",
4163 lambda : b"\n",
4164 lambda : b"B",
4165 self._raise_eintr,
4166 lambda : b"b",
4167 lambda : b"",
4168 ])
4169 fo = mock_sock._textiowrap_for_test(buffering=0)
4170 self.assertEqual(fo.readline(size), b"a\n")
4171 self.assertEqual(fo.readline(size), b"Bb")
4172
4173 def test_no_buffer(self):
4174 self._test_readline_no_buffer()
4175 self._test_readline_no_buffer(size=4)
4176 self._test_read(buffering=0)
4177 self._test_read(size=100, buffering=0)
4178 4068
4179 4069
4180 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): 4070 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
4181 4071
4182 """Repeat the tests from FileObjectClassTestCase with bufsize==0. 4072 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
4183 4073
4184 In this case (and in this case only), it should be possible to 4074 In this case (and in this case only), it should be possible to
4185 create a file object, read a line from it, create another file 4075 create a file object, read a line from it, create another file
4186 object, read another line from it, without loss of data in the 4076 object, read another line from it, without loss of data in the
4187 first file object's buffer. Note that http.client relies on this 4077 first file object's buffer. Note that http.client relies on this
(...skipping 1203 matching lines...) Expand 10 before | Expand all | Expand 10 after
5391 return getattr(sock, "_sendfile_use_sendfile") 5281 return getattr(sock, "_sendfile_use_sendfile")
5392 5282
5393 5283
5394 def test_main(): 5284 def test_main():
5395 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, 5285 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
5396 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeo utTest ] 5286 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeo utTest ]
5397 5287
5398 tests.extend([ 5288 tests.extend([
5399 NonBlockingTCPTests, 5289 NonBlockingTCPTests,
5400 FileObjectClassTestCase, 5290 FileObjectClassTestCase,
5401 FileObjectInterruptedTestCase,
5402 UnbufferedFileObjectClassTestCase, 5291 UnbufferedFileObjectClassTestCase,
5403 LineBufferedFileObjectClassTestCase, 5292 LineBufferedFileObjectClassTestCase,
5404 SmallBufferedFileObjectClassTestCase, 5293 SmallBufferedFileObjectClassTestCase,
5405 UnicodeReadFileObjectClassTestCase, 5294 UnicodeReadFileObjectClassTestCase,
5406 UnicodeWriteFileObjectClassTestCase, 5295 UnicodeWriteFileObjectClassTestCase,
5407 UnicodeReadWriteFileObjectClassTestCase, 5296 UnicodeReadWriteFileObjectClassTestCase,
5408 NetworkConnectionNoServer, 5297 NetworkConnectionNoServer,
5409 NetworkConnectionAttributesTest, 5298 NetworkConnectionAttributesTest,
5410 NetworkConnectionBehaviourTest, 5299 NetworkConnectionBehaviourTest,
5411 ContextManagersTest, 5300 ContextManagersTest,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
5446 SendfileUsingSendTest, 5335 SendfileUsingSendTest,
5447 SendfileUsingSendfileTest, 5336 SendfileUsingSendfileTest,
5448 ]) 5337 ])
5449 5338
5450 thread_info = support.threading_setup() 5339 thread_info = support.threading_setup()
5451 support.run_unittest(*tests) 5340 support.run_unittest(*tests)
5452 support.threading_cleanup(*thread_info) 5341 support.threading_cleanup(*thread_info)
5453 5342
5454 if __name__ == "__main__": 5343 if __name__ == "__main__":
5455 test_main() 5344 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+