annotate Lib/test/test_socket.py @ 65989:458b05abf987

Output served URL when running ssl_servers
author Antoine Pitrou <solipsis@pitrou.net>
date Fri, 05 Nov 2010 20:26:59 +0000
parents e213e465a2dc
children c20ade63f3db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 58415
diff changeset
1 #!/usr/bin/env python3
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
2
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
3 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
4 from test import support
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
5
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
6 import errno
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
7 import io
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
8 import socket
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
9 import select
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
10 import time
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44782
diff changeset
11 import traceback
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46370
diff changeset
12 import queue
25152
0c9dfd5a06d4 Skip UDP testing for MacPython (for now), it hangs. This may be due to
Jack Jansen <jack.jansen@cwi.nl>
parents: 24772
diff changeset
13 import sys
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43954
diff changeset
14 import os
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43954
diff changeset
15 import array
65604
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
16 import platform
64534
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
17 import contextlib
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
18 from weakref import proxy
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
19 import signal
64959
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
20 import math
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
21 try:
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
22 import fcntl
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
23 except ImportError:
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
24 fcntl = False
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
25
63997
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
26 def try_address(host, port=0, family=socket.AF_INET):
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
27 """Try to bind a socket on the given host:port and return True
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
28 if that has been possible."""
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
29 try:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
30 sock = socket.socket(family, socket.SOCK_STREAM)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
31 sock.bind((host, port))
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
32 except (socket.error, socket.gaierror):
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
33 return False
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
34 else:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
35 sock.close()
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
36 return True
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
37
65606
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
38 def linux_version():
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
39 try:
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
40 # platform.release() is something like '2.6.33.7-desktop-2mnb'
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
41 version_string = platform.release().split('-')[0]
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
42 return tuple(map(int, version_string.split('.')))
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
43 except ValueError:
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
44 return 0, 0, 0
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
45
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
46 HOST = support.HOST
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
47 MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf8') ## test unicode string and carriage return
63997
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
48 SUPPORTS_IPV6 = socket.has_ipv6 and try_address('::1', family=socket.AF_INET6)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
49
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
50 try:
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
51 import _thread as thread
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
52 import threading
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
53 except ImportError:
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
54 thread = None
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
55 threading = None
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
56
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
57 class SocketTCPTest(unittest.TestCase):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
58
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
59 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
60 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
61 self.port = support.bind_port(self.serv)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
62 self.serv.listen(1)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
63
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
64 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
65 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
66 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
67
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
68 class SocketUDPTest(unittest.TestCase):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
69
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
70 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
71 self.serv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
72 self.port = support.bind_port(self.serv)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
73
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
74 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
75 self.serv.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
76 self.serv = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
77
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
78 class ThreadableTest:
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
79 """Threadable Test class
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
80
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
81 The ThreadableTest class makes it easy to create a threaded
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
82 client/server pair from an existing unit test. To create a
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
83 new threaded class from an existing unit test, use multiple
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
84 inheritance:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
85
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
86 class NewClass (OldClass, ThreadableTest):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
87 pass
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
88
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
89 This class defines two new fixture functions with obvious
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
90 purposes for overriding:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
91
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
92 clientSetUp ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
93 clientTearDown ()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
94
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
95 Any new test functions within the class must then define
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
96 tests in pairs, where the test name is preceeded with a
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
97 '_' to indicate the client portion of the test. Ex:
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
98
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
99 def testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
100 # Server portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
101
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
102 def _testFoo(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
103 # Client portion
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
104
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
105 Any exceptions raised by the clients during their tests
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
106 are caught and transferred to the main thread to alert
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
107 the testing framework.
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
108
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
109 Note, the server setup function cannot call any blocking
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
110 functions that rely on the client thread during setup,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
111 unless serverExplicitReady() is called just before
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
112 the blocking call (such as in setting up a client/server
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
113 connection and performing the accept() in setUp().
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
114 """
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
115
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
116 def __init__(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
117 # Swap the true setup function
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
118 self.__setUp = self.setUp
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
119 self.__tearDown = self.tearDown
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
120 self.setUp = self._setUp
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
121 self.tearDown = self._tearDown
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
122
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
123 def serverExplicitReady(self):
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
124 """This method allows the server to explicitly indicate that
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
125 it wants the client thread to proceed. This is useful if the
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
126 server is about to execute a blocking routine that is
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
127 dependent upon the client thread during its setup routine."""
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
128 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
129
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
130 def _setUp(self):
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
131 self.server_ready = threading.Event()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
132 self.client_ready = threading.Event()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
133 self.done = threading.Event()
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46370
diff changeset
134 self.queue = queue.Queue(1)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
135
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
136 # Do some munging to start the client test.
23815
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
137 methodname = self.id()
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
138 i = methodname.rfind('.')
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
139 methodname = methodname[i+1:]
203d1412a7e8 Major overhaul of timeout sockets:
Guido van Rossum <guido@python.org>
parents: 23814
diff changeset
140 test_method = getattr(self, '_' + methodname)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
141 self.client_thread = thread.start_new_thread(
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
142 self.clientRun, (test_method,))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
143
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
144 self.__setUp()
47615
045f83a1b864 Merged revisions 64125 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47559
diff changeset
145 if not self.server_ready.is_set():
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
146 self.server_ready.set()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
147 self.client_ready.wait()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
148
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
149 def _tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
150 self.__tearDown()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
151 self.done.wait()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
152
44675
627f2e2273b9 Remove Queue.empty() and Queue.full() in favor of using qsize() or trapping the Empty and Full exceptions.
Raymond Hettinger <python@rcn.com>
parents: 44604
diff changeset
153 if self.queue.qsize():
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
154 exc = self.queue.get()
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
155 raise exc
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
156
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
157 def clientRun(self, test_func):
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
158 self.server_ready.wait()
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
159 self.client_ready.set()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
160 self.clientSetUp()
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41809
diff changeset
161 if not hasattr(test_func, '__call__'):
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42637
diff changeset
162 raise TypeError("test_func must be a callable function")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
163 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
164 test_func()
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
165 except BaseException as e:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
166 self.queue.put(e)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
167 finally:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
168 self.clientTearDown()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
169
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
170 def clientSetUp(self):
43014
9834301a2fd3 Raise statement normalization in Lib/test/.
Collin Winter <collinw@gmail.com>
parents: 42637
diff changeset
171 raise NotImplementedError("clientSetUp must be implemented.")
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
172
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
173 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
174 self.done.set()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
175 thread.exit()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
176
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
177 class ThreadedTCPSocketTest(SocketTCPTest, ThreadableTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
178
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
179 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
180 SocketTCPTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
181 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
182
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
183 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
184 self.cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
185
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
186 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
187 self.cli.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
188 self.cli = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
189 ThreadableTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
190
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
191 class ThreadedUDPSocketTest(SocketUDPTest, ThreadableTest):
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
192
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
193 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
194 SocketUDPTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
195 ThreadableTest.__init__(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
196
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
197 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
198 self.cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
199
65925
8a5f0873d738 Fix three ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65882
diff changeset
200 def clientTearDown(self):
8a5f0873d738 Fix three ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65882
diff changeset
201 self.cli.close()
8a5f0873d738 Fix three ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65882
diff changeset
202 self.cli = None
8a5f0873d738 Fix three ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65882
diff changeset
203 ThreadableTest.clientTearDown(self)
8a5f0873d738 Fix three ResourceWarnings.
Brian Curtin <brian.curtin@gmail.com>
parents: 65882
diff changeset
204
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
205 class SocketConnectedTest(ThreadedTCPSocketTest):
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
206 """Socket tests for client-server connection.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
207
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
208 self.cli_conn is a client socket connected to the server. The
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
209 setUp() method guarantees that it is connected to the server.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
210 """
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
211
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
212 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
213 ThreadedTCPSocketTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
214
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
215 def setUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
216 ThreadedTCPSocketTest.setUp(self)
23909
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
217 # Indicate explicitly we're ready for the client thread to
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
218 # proceed and then perform the blocking call to accept
b16d4be6a232 Michael fixed the race conditions and removed the sleeps.
Guido van Rossum <guido@python.org>
parents: 23831
diff changeset
219 self.serverExplicitReady()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
220 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
221 self.cli_conn = conn
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
222
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
223 def tearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
224 self.cli_conn.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
225 self.cli_conn = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
226 ThreadedTCPSocketTest.tearDown(self)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
227
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
228 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
229 ThreadedTCPSocketTest.clientSetUp(self)
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
230 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
231 self.serv_conn = self.cli
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
232
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
233 def clientTearDown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
234 self.serv_conn.close()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
235 self.serv_conn = None
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
236 ThreadedTCPSocketTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
237
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
238 class SocketPairTest(unittest.TestCase, ThreadableTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
239
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
240 def __init__(self, methodName='runTest'):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
241 unittest.TestCase.__init__(self, methodName=methodName)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
242 ThreadableTest.__init__(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
243
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
244 def setUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
245 self.serv, self.cli = socket.socketpair()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
246
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
247 def tearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
248 self.serv.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
249 self.serv = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
250
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
251 def clientSetUp(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
252 pass
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
253
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
254 def clientTearDown(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
255 self.cli.close()
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
256 self.cli = None
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
257 ThreadableTest.clientTearDown(self)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
258
33076
836e77266f97 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33063
diff changeset
259
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
260 #######################################################################
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
261 ## Begin Tests
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
262
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
263 class GeneralModuleTests(unittest.TestCase):
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
264
41941
e4ab08039c75 Simplify socket_repr() by using PyUnicode_FromFormat()
Walter Dörwald <walter@livinglogic.de>
parents: 41824
diff changeset
265 def test_repr(self):
e4ab08039c75 Simplify socket_repr() by using PyUnicode_FromFormat()
Walter Dörwald <walter@livinglogic.de>
parents: 41824
diff changeset
266 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
267 self.addCleanup(s.close)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
268 self.assertTrue(repr(s).startswith("<socket.socket object"))
41941
e4ab08039c75 Simplify socket_repr() by using PyUnicode_FromFormat()
Walter Dörwald <walter@livinglogic.de>
parents: 41824
diff changeset
269
32160
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
270 def test_weakref(self):
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
271 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
272 p = proxy(s)
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
273 self.assertEqual(p.fileno(), s.fileno())
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
274 s.close()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
275 s = None
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
276 try:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
277 p.fileno()
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
278 except ReferenceError:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
279 pass
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
280 else:
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
281 self.fail('Socket proxy still exists')
aec9c98a47d2 Add weakref support to sockets and re pattern objects.
Raymond Hettinger <python@rcn.com>
parents: 31833
diff changeset
282
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
283 def testSocketError(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
284 # Testing socket module exceptions
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
285 def raise_error(*args, **kwargs):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
286 raise socket.error
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
287 def raise_herror(*args, **kwargs):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
288 raise socket.herror
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
289 def raise_gaierror(*args, **kwargs):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
290 raise socket.gaierror
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
291 self.assertRaises(socket.error, raise_error,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
292 "Error raising socket exception.")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
293 self.assertRaises(socket.error, raise_herror,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
294 "Error raising socket exception.")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
295 self.assertRaises(socket.error, raise_gaierror,
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
296 "Error raising socket exception.")
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
297
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
298 def testCrucialConstants(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
299 # Testing for mission critical constants
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
300 socket.AF_INET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
301 socket.SOCK_STREAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
302 socket.SOCK_DGRAM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
303 socket.SOCK_RAW
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
304 socket.SOCK_RDM
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
305 socket.SOCK_SEQPACKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
306 socket.SOL_SOCKET
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
307 socket.SO_REUSEADDR
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
308
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
309 def testHostnameRes(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
310 # Testing hostname resolution mechanisms
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
311 hostname = socket.gethostname()
26799
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
312 try:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
313 ip = socket.gethostbyname(hostname)
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
314 except socket.error:
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
315 # Probably name lookup wasn't set up right; skip this test
507aa96a128f Skip testHostnameRes() if gethostbyname() raises an exception.
Guido van Rossum <guido@python.org>
parents: 25285
diff changeset
316 return
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
317 self.assertTrue(ip.find('.') >= 0, "Error resolving host to ip.")
26800
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
318 try:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
319 hname, aliases, ipaddrs = socket.gethostbyaddr(ip)
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
320 except socket.error:
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
321 # Probably a similar problem as above; skip this test
b2b4797052c6 Also skip testHostnameRes() if gethostbyaddr() raises an exception.
Guido van Rossum <guido@python.org>
parents: 26799
diff changeset
322 return
34739
0c82a7ef9554 Fix test_socket's test for socket.getfqdn() to also accept the result from
Brett Cannon <bcannon@gmail.com>
parents: 34014
diff changeset
323 all_host_names = [hostname, hname] + aliases
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
324 fqhn = socket.getfqdn(ip)
23831
f7306ae8662d Temporarily disable the timeout and socket tests.
Guido van Rossum <guido@python.org>
parents: 23819
diff changeset
325 if not fqhn in all_host_names:
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
326 self.fail("Error testing host resolution mechanisms. (fqdn: %s, all: %s)" % (fqhn, repr(all_host_names)))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
327
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
328 def testRefCountGetNameInfo(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
329 # Testing reference count for getnameinfo
23799
2ad13ee6d850 Don't test for Java, test for sys.getrefcount.
Guido van Rossum <guido@python.org>
parents: 23798
diff changeset
330 if hasattr(sys, "getrefcount"):
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
331 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
332 # On some versions, this loses a reference
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
333 orig = sys.getrefcount(__name__)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
334 socket.getnameinfo(__name__,0)
56367
41bb5e2dc947 Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 56252
diff changeset
335 except TypeError:
39648
efe3f2c0da5e Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
336 if sys.getrefcount(__name__) != orig:
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
337 self.fail("socket.getnameinfo loses a reference")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
338
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
339 def testInterpreterCrash(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
340 # Making sure getnameinfo doesn't crash the interpreter
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
341 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
342 # On some versions, this crashes the interpreter.
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
343 socket.getnameinfo(('x', 0, 0, 0), 0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
344 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
345 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
346
25285
4d6f8204234a Since it tests both ntohl and ntohs, the test should not be called
Guido van Rossum <guido@python.org>
parents: 25256
diff changeset
347 def testNtoH(self):
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
348 # This just checks that htons etc. are their own inverse,
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
349 # when looking at the lower 16 or 32 bits.
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
350 sizes = {socket.htonl: 32, socket.ntohl: 32,
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
351 socket.htons: 16, socket.ntohs: 16}
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
352 for func, size in sizes.items():
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40638
diff changeset
353 mask = (1<<size) - 1
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
354 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
355 self.assertEqual(i & mask, func(func(i&mask)) & mask)
24450
747fa65a9b47 Repair testNtoH for large long arguments.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24375
diff changeset
356
25256
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
357 swapped = func(mask)
04a541e68964 Maybe this fixes test_socket on 64-bit Linux.
Guido van Rossum <guido@python.org>
parents: 25152
diff changeset
358 self.assertEqual(swapped & mask, mask)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40638
diff changeset
359 self.assertRaises(OverflowError, func, 1<<34)
24375
89ce62126664 Extended socket.htonl and ntohl to accept longs.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24374
diff changeset
360
40638
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
361 def testNtoHErrors(self):
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40638
diff changeset
362 good_values = [ 1, 2, 3, 1, 2, 3 ]
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 40638
diff changeset
363 bad_values = [ -1, -2, -3, -1, -2, -3 ]
40638
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
364 for k in good_values:
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
365 socket.ntohl(k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
366 socket.ntohs(k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
367 socket.htonl(k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
368 socket.htons(k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
369 for k in bad_values:
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
370 self.assertRaises(OverflowError, socket.ntohl, k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
371 self.assertRaises(OverflowError, socket.ntohs, k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
372 self.assertRaises(OverflowError, socket.htonl, k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
373 self.assertRaises(OverflowError, socket.htons, k)
86ce1df6a88e Merged revisions 53434 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
374
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
375 def testGetServBy(self):
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
376 eq = self.assertEqual
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
377 # Find one service that exists, then check all the related interfaces.
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
378 # I've ordered this by protocols that have both a tcp and udp
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
379 # protocol, at least for modern Linuxes.
65416
443ab2cbabcb fix for netbsd.
Gregory P. Smith <greg@mad-scientist.com>
parents: 65354
diff changeset
380 if (sys.platform.startswith('linux') or
443ab2cbabcb fix for netbsd.
Gregory P. Smith <greg@mad-scientist.com>
parents: 65354
diff changeset
381 sys.platform.startswith('freebsd') or
443ab2cbabcb fix for netbsd.
Gregory P. Smith <greg@mad-scientist.com>
parents: 65354
diff changeset
382 sys.platform.startswith('netbsd') or
443ab2cbabcb fix for netbsd.
Gregory P. Smith <greg@mad-scientist.com>
parents: 65354
diff changeset
383 sys.platform == 'darwin'):
32630
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
384 # avoid the 'echo' service on this platform, as there is an
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
385 # assumption breaking non-standard port/protocol entry
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
386 services = ('daytime', 'qotd', 'domain')
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
387 else:
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
388 services = ('echo', 'daytime', 'domain')
059dae06585b FreeBSD's services file contains an additional echo service entry, with
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 32508
diff changeset
389 for service in services:
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
390 try:
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
391 port = socket.getservbyname(service, 'tcp')
24485
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
392 break
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
393 except socket.error:
f05a311d7988 testGetServByName shouldn't check for getservbyname - the socket module
Skip Montanaro <skip@pobox.com>
parents: 24470
diff changeset
394 pass
31503
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
395 else:
eb48b2503f20 Fix indentation error in testGetServByName and rewrite loop to avoid clumsy
Skip Montanaro <skip@pobox.com>
parents: 29662
diff changeset
396 raise socket.error
32429
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
397 # Try same call with optional protocol omitted
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
398 port2 = socket.getservbyname(service)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
399 eq(port, port2)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
400 # Try udp, but don't barf it it doesn't exist
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
401 try:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
402 udpport = socket.getservbyname(service, 'udp')
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
403 except socket.error:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
404 udpport = None
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
405 else:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
406 eq(udpport, port)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
407 # Now make sure the lookup by port returns the same service name
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
408 eq(socket.getservbyport(port2), service)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
409 eq(socket.getservbyport(port, 'tcp'), service)
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
410 if udpport is not None:
d8abf582f893 Added socket.getservbyport(), and make its second argument and that of
Barry Warsaw <barry@python.org>
parents: 32160
diff changeset
411 eq(socket.getservbyport(udpport, 'udp'), service)
51382
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
412 # Make sure getservbyport does not accept out of range ports.
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
413 self.assertRaises(OverflowError, socket.getservbyport, -1)
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
414 self.assertRaises(OverflowError, socket.getservbyport, 65536)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
415
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
416 def testDefaultTimeout(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
417 # Testing default timeout
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
418 # The default timeout should initially be None
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
419 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
420 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
421 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
422 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
423
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
424 # Set the default timeout to 10, and see if it propagates
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
425 socket.setdefaulttimeout(10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
426 self.assertEqual(socket.getdefaulttimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
427 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
428 self.assertEqual(s.gettimeout(), 10)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
429 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
430
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
431 # Reset the default timeout to None, and see if it propagates
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
432 socket.setdefaulttimeout(None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
433 self.assertEqual(socket.getdefaulttimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
434 s = socket.socket()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
435 self.assertEqual(s.gettimeout(), None)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
436 s.close()
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
437
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
438 # Check that setting it to an invalid value raises ValueError
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
439 self.assertRaises(ValueError, socket.setdefaulttimeout, -1)
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
440
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
441 # Check that setting it to an invalid type raises TypeError
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
442 self.assertRaises(TypeError, socket.setdefaulttimeout, "spam")
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
443
51579
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
444 def testIPv4_inet_aton_fourbytes(self):
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
445 if not hasattr(socket, 'inet_aton'):
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
446 return # No inet_aton, nothing to check
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
447 # Test that issue1008086 and issue767150 are fixed.
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
448 # It must return 4 bytes.
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
449 self.assertEquals(b'\x00'*4, socket.inet_aton('0.0.0.0'))
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
450 self.assertEquals(b'\xff'*4, socket.inet_aton('255.255.255.255'))
648e07dd0825 Merged revisions 69364-69365,69409-69410,69413,69417,69435,69442,69447,69495,69519-69521 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51382
diff changeset
451
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
452 def testIPv4toString(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
453 if not hasattr(socket, 'inet_pton'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
454 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
455 from socket import inet_aton as f, inet_pton, AF_INET
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
456 g = lambda a: inet_pton(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
457
41770
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
458 self.assertEquals(b'\x00\x00\x00\x00', f('0.0.0.0'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
459 self.assertEquals(b'\xff\x00\xff\x00', f('255.0.255.0'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
460 self.assertEquals(b'\xaa\xaa\xaa\xaa', f('170.170.170.170'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
461 self.assertEquals(b'\x01\x02\x03\x04', f('1.2.3.4'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
462 self.assertEquals(b'\xff\xff\xff\xff', f('255.255.255.255'))
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
463
41770
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
464 self.assertEquals(b'\x00\x00\x00\x00', g('0.0.0.0'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
465 self.assertEquals(b'\xff\x00\xff\x00', g('255.0.255.0'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
466 self.assertEquals(b'\xaa\xaa\xaa\xaa', g('170.170.170.170'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
467 self.assertEquals(b'\xff\xff\xff\xff', g('255.255.255.255'))
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
468
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
469 def testIPv6toString(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
470 if not hasattr(socket, 'inet_pton'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
471 return # No inet_pton() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
472 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
473 from socket import inet_pton, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
474 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
475 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
476 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
477 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
478 f = lambda a: inet_pton(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
479
42637
ff5adb3374ad Fix the IPv6 pton/ntop tests. Why weren't these failing on Linux? No IPv6?
Guido van Rossum <guido@python.org>
parents: 42596
diff changeset
480 self.assertEquals(b'\x00' * 16, f('::'))
ff5adb3374ad Fix the IPv6 pton/ntop tests. Why weren't these failing on Linux? No IPv6?
Guido van Rossum <guido@python.org>
parents: 42596
diff changeset
481 self.assertEquals(b'\x00' * 16, f('0::0'))
ff5adb3374ad Fix the IPv6 pton/ntop tests. Why weren't these failing on Linux? No IPv6?
Guido van Rossum <guido@python.org>
parents: 42596
diff changeset
482 self.assertEquals(b'\x00\x01' + b'\x00' * 14, f('1::'))
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
483 self.assertEquals(
42169
e38047550383 Fix test_socket.py. Simple str/bytes issue with IPV6 addresse.
Guido van Rossum <guido@python.org>
parents: 41941
diff changeset
484 b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
485 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
486 )
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
487
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
488 def testStringToIPv4(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
489 if not hasattr(socket, 'inet_ntop'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
490 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
491 from socket import inet_ntoa as f, inet_ntop, AF_INET
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
492 g = lambda a: inet_ntop(AF_INET, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
493
41770
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
494 self.assertEquals('1.0.1.0', f(b'\x01\x00\x01\x00'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
495 self.assertEquals('170.85.170.85', f(b'\xaa\x55\xaa\x55'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
496 self.assertEquals('255.255.255.255', f(b'\xff\xff\xff\xff'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
497 self.assertEquals('1.2.3.4', f(b'\x01\x02\x03\x04'))
29087
526f37fb380d Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28917
diff changeset
498
41770
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
499 self.assertEquals('1.0.1.0', g(b'\x01\x00\x01\x00'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
500 self.assertEquals('170.85.170.85', g(b'\xaa\x55\xaa\x55'))
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
501 self.assertEquals('255.255.255.255', g(b'\xff\xff\xff\xff'))
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
502
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
503 def testStringToIPv6(self):
28833
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
504 if not hasattr(socket, 'inet_ntop'):
fa159fef2730 Skip testing inet_ntop() an inet_pton() if they aren't defined.
Guido van Rossum <guido@python.org>
parents: 28818
diff changeset
505 return # No inet_ntop() on this platform
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
506 try:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
507 from socket import inet_ntop, AF_INET6, has_ipv6
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
508 if not has_ipv6:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
509 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
510 except ImportError:
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
511 return
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
512 f = lambda a: inet_ntop(AF_INET6, a)
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
513
42637
ff5adb3374ad Fix the IPv6 pton/ntop tests. Why weren't these failing on Linux? No IPv6?
Guido van Rossum <guido@python.org>
parents: 42596
diff changeset
514 self.assertEquals('::', f(b'\x00' * 16))
ff5adb3374ad Fix the IPv6 pton/ntop tests. Why weren't these failing on Linux? No IPv6?
Guido van Rossum <guido@python.org>
parents: 42596
diff changeset
515 self.assertEquals('::1', f(b'\x00' * 15 + b'\x01'))
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
516 self.assertEquals(
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
517 'aef:b01:506:1001:ffff:9997:55:170',
42169
e38047550383 Fix test_socket.py. Simple str/bytes issue with IPV6 addresse.
Guido van Rossum <guido@python.org>
parents: 41941
diff changeset
518 f(b'\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
28818
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
519 )
57e61588571b Patch by Jp Calderone:
Guido van Rossum <guido@python.org>
parents: 26800
diff changeset
520
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
521 # XXX The following don't test module-level functionality...
24293
640370f08e2a Add default timeout functionality. This adds setdefaulttimeout() and
Guido van Rossum <guido@python.org>
parents: 23931
diff changeset
522
51382
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
523 def _get_unused_port(self, bind_address='0.0.0.0'):
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
524 """Use a temporary socket to elicit an unused ephemeral port.
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
525
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
526 Args:
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
527 bind_address: Hostname or IP address to search for a port on.
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
528
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
529 Returns: A most likely to be unused port.
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
530 """
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
531 tempsock = socket.socket()
51382
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
532 tempsock.bind((bind_address, 0))
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
533 host, port = tempsock.getsockname()
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
534 tempsock.close()
51382
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
535 return port
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
536
51382
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
537 def testSockName(self):
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
538 # Testing getsockname()
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
539 port = self._get_unused_port()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
540 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
541 self.addCleanup(sock.close)
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
542 sock.bind(("0.0.0.0", port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
543 name = sock.getsockname()
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
544 # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
545 # it reasonable to get the host's addr in addition to 0.0.0.0.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
546 # At least for eCos. This is required for the S/390 to pass.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
547 my_ip_addr = socket.gethostbyname(socket.gethostname())
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 58146
diff changeset
548 self.assertIn(name[0], ("0.0.0.0", my_ip_addr), '%s invalid' % name[0])
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
549 self.assertEqual(name[1], port)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
550
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
551 def testGetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
552 # Testing getsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
553 # We know a socket should start without reuse==0
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
554 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
555 self.addCleanup(sock.close)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
556 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
557 self.assertFalse(reuse != 0, "initial mode is reuse")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
558
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
559 def testSetSockOpt(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
560 # Testing setsockopt()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
561 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
562 self.addCleanup(sock.close)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
563 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
564 reuse = sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
565 self.assertFalse(reuse == 0, "failed to set reuse mode")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
566
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
567 def testSendAfterClose(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
568 # testing send() after close() with timeout
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
569 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
570 sock.settimeout(1)
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
571 sock.close()
43954
092b5306983f Convert the socket module to insist on bytes for input, and to return bytes
Guido van Rossum <guido@python.org>
parents: 43881
diff changeset
572 self.assertRaises(socket.error, sock.send, b"spam")
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
573
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
574 def testNewAttributes(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
575 # testing .family, .type and .protocol
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
576 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
577 self.assertEqual(sock.family, socket.AF_INET)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
578 self.assertEqual(sock.type, socket.SOCK_STREAM)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
579 self.assertEqual(sock.proto, 0)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
580 sock.close()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
581
51382
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
582 def test_getsockaddrarg(self):
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
583 host = '0.0.0.0'
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
584 port = self._get_unused_port(bind_address=host)
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
585 big_port = port + 65536
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
586 neg_port = port - 65536
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
587 sock = socket.socket()
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
588 try:
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
589 self.assertRaises(OverflowError, sock.bind, (host, big_port))
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
590 self.assertRaises(OverflowError, sock.bind, (host, neg_port))
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
591 sock.bind((host, port))
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
592 finally:
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
593 sock.close()
7e28a2b0c1b1 Merged revisions 69129-69131,69139-69140,69143,69154-69159,69169,69288-69289,69293,69297-69301,69348 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50800
diff changeset
594
65975
e213e465a2dc Add socket cleanup for ResourceWarning and update test to use skip decorator
Brian Curtin <brian.curtin@gmail.com>
parents: 65925
diff changeset
595 @unittest.skipUnless(os.name == "nt", "Windows specific")
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43954
diff changeset
596 def test_sock_ioctl(self):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
597 self.assertTrue(hasattr(socket.socket, 'ioctl'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
598 self.assertTrue(hasattr(socket, 'SIO_RCVALL'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
599 self.assertTrue(hasattr(socket, 'RCVALL_ON'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
600 self.assertTrue(hasattr(socket, 'RCVALL_OFF'))
56252
debb245ebb40 http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55593
diff changeset
601 self.assertTrue(hasattr(socket, 'SIO_KEEPALIVE_VALS'))
debb245ebb40 http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55593
diff changeset
602 s = socket.socket()
65975
e213e465a2dc Add socket cleanup for ResourceWarning and update test to use skip decorator
Brian Curtin <brian.curtin@gmail.com>
parents: 65925
diff changeset
603 self.addCleanup(s.close)
56252
debb245ebb40 http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55593
diff changeset
604 self.assertRaises(ValueError, s.ioctl, -1, None)
debb245ebb40 http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 55593
diff changeset
605 s.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 100, 100))
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43954
diff changeset
606
63997
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
607 def testGetaddrinfo(self):
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
608 try:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
609 socket.getaddrinfo('localhost', 80)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
610 except socket.gaierror as err:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
611 if err.errno == socket.EAI_SERVICE:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
612 # see http://bugs.python.org/issue1282647
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
613 self.skipTest("buggy libc version")
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
614 raise
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
615 # len of every sequence is supposed to be == 5
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
616 for info in socket.getaddrinfo(HOST, None):
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
617 self.assertEqual(len(info), 5)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
618 # host can be a domain name, a string representation of an
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
619 # IPv4/v6 address or None
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
620 socket.getaddrinfo('localhost', 80)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
621 socket.getaddrinfo('127.0.0.1', 80)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
622 socket.getaddrinfo(None, 80)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
623 if SUPPORTS_IPV6:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
624 socket.getaddrinfo('::1', 80)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
625 # port can be a string service name such as "http", a numeric
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
626 # port number or None
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
627 socket.getaddrinfo(HOST, "http")
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
628 socket.getaddrinfo(HOST, 80)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
629 socket.getaddrinfo(HOST, None)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
630 # test family and socktype filters
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
631 infos = socket.getaddrinfo(HOST, None, socket.AF_INET)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
632 for family, _, _, _, _ in infos:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
633 self.assertEqual(family, socket.AF_INET)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
634 infos = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
635 for _, socktype, _, _, _ in infos:
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
636 self.assertEqual(socktype, socket.SOCK_STREAM)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
637 # test proto and flags arguments
64059
92f7877088af fix getaddrinfo test failure on OSX caused by AI_CANNAME erroneously used as the value for 'proto'
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63997
diff changeset
638 socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
63997
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
639 socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
640 # a server willing to support both IPv4 and IPv6 will
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
641 # usually do this
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
642 socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
643 socket.AI_PASSIVE)
64113
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
644 # test keyword arguments
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
645 a = socket.getaddrinfo(HOST, None)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
646 b = socket.getaddrinfo(host=HOST, port=None)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
647 self.assertEqual(a, b)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
648 a = socket.getaddrinfo(HOST, None, socket.AF_INET)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
649 b = socket.getaddrinfo(HOST, None, family=socket.AF_INET)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
650 self.assertEqual(a, b)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
651 a = socket.getaddrinfo(HOST, None, 0, socket.SOCK_STREAM)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
652 b = socket.getaddrinfo(HOST, None, type=socket.SOCK_STREAM)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
653 self.assertEqual(a, b)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
654 a = socket.getaddrinfo(HOST, None, 0, 0, socket.SOL_TCP)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
655 b = socket.getaddrinfo(HOST, None, proto=socket.SOL_TCP)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
656 self.assertEqual(a, b)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
657 a = socket.getaddrinfo(HOST, None, 0, 0, 0, socket.AI_PASSIVE)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
658 b = socket.getaddrinfo(HOST, None, flags=socket.AI_PASSIVE)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
659 self.assertEqual(a, b)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
660 a = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
661 socket.AI_PASSIVE)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
662 b = socket.getaddrinfo(host=None, port=0, family=socket.AF_UNSPEC,
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
663 type=socket.SOCK_STREAM, proto=0,
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
664 flags=socket.AI_PASSIVE)
ca2a859e9bf6 fix issue #8866: parameters passed to socket.getaddrinfo can now be specified as single keyword arguments.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64059
diff changeset
665 self.assertEqual(a, b)
63997
b7ab77fd27b7 fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 63890
diff changeset
666
64277
2ccd043aad5c Issue #1027206: getnameinfo is now restricted to numeric addresses as input.
Martin v. Löwis <martin@v.loewis.de>
parents: 64241
diff changeset
667 def test_getnameinfo(self):
2ccd043aad5c Issue #1027206: getnameinfo is now restricted to numeric addresses as input.
Martin v. Löwis <martin@v.loewis.de>
parents: 64241
diff changeset
668 # only IP addresses are allowed
2ccd043aad5c Issue #1027206: getnameinfo is now restricted to numeric addresses as input.
Martin v. Löwis <martin@v.loewis.de>
parents: 64241
diff changeset
669 self.assertRaises(socket.error, socket.getnameinfo, ('mail.python.org',0), 0)
2ccd043aad5c Issue #1027206: getnameinfo is now restricted to numeric addresses as input.
Martin v. Löwis <martin@v.loewis.de>
parents: 64241
diff changeset
670
64593
6f14f02359e8 Skip socket tests that require the network, if the network resource is not enabled
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64574
diff changeset
671 @unittest.skipUnless(support.is_resource_enabled('network'),
6f14f02359e8 Skip socket tests that require the network, if the network resource is not enabled
Daniel Stutzbach <daniel@stutzbachenterprises.com>
parents: 64574
diff changeset
672 'network is not enabled')
64230
35304c2fe9e0 Issue #1027206: Support IDNA in gethostbyname, gethostbyname_ex and
Martin v. Löwis <martin@v.loewis.de>
parents: 64113
diff changeset
673 def test_idna(self):
64442
5997ae2eab37 Issue #9777: test_idna requires the "network" resource
Antoine Pitrou <solipsis@pitrou.net>
parents: 64391
diff changeset
674 support.requires('network')
64230
35304c2fe9e0 Issue #1027206: Support IDNA in gethostbyname, gethostbyname_ex and
Martin v. Löwis <martin@v.loewis.de>
parents: 64113
diff changeset
675 # these should all be successful
35304c2fe9e0 Issue #1027206: Support IDNA in gethostbyname, gethostbyname_ex and
Martin v. Löwis <martin@v.loewis.de>
parents: 64113
diff changeset
676 socket.gethostbyname('испытание.python.org')
35304c2fe9e0 Issue #1027206: Support IDNA in gethostbyname, gethostbyname_ex and
Martin v. Löwis <martin@v.loewis.de>
parents: 64113
diff changeset
677 socket.gethostbyname_ex('испытание.python.org')
64241
7e1e343b708b Explicitly pass socket type and family for getaddrinfo,
Martin v. Löwis <martin@v.loewis.de>
parents: 64231
diff changeset
678 socket.getaddrinfo('испытание.python.org',0,socket.AF_UNSPEC,socket.SOCK_STREAM)
7e1e343b708b Explicitly pass socket type and family for getaddrinfo,
Martin v. Löwis <martin@v.loewis.de>
parents: 64231
diff changeset
679 # this may not work if the forward lookup choses the IPv6 address, as that doesn't
7e1e343b708b Explicitly pass socket type and family for getaddrinfo,
Martin v. Löwis <martin@v.loewis.de>
parents: 64231
diff changeset
680 # have a reverse entry yet
7e1e343b708b Explicitly pass socket type and family for getaddrinfo,
Martin v. Löwis <martin@v.loewis.de>
parents: 64231
diff changeset
681 # socket.gethostbyaddr('испытание.python.org')
44535
420fe6d7d5f4 Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43954
diff changeset
682
64959
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
683 def check_sendall_interrupted(self, with_timeout):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
684 # socketpair() is not stricly required, but it makes things easier.
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
685 if not hasattr(signal, 'alarm') or not hasattr(socket, 'socketpair'):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
686 self.skipTest("signal.alarm and socket.socketpair required for this test")
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
687 # Our signal handlers clobber the C errno by calling a math function
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
688 # with an invalid domain value.
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
689 def ok_handler(*args):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
690 self.assertRaises(ValueError, math.acosh, 0)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
691 def raising_handler(*args):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
692 self.assertRaises(ValueError, math.acosh, 0)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
693 1 // 0
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
694 c, s = socket.socketpair()
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
695 old_alarm = signal.signal(signal.SIGALRM, raising_handler)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
696 try:
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
697 if with_timeout:
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
698 # Just above the one second minimum for signal.alarm
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
699 c.settimeout(1.5)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
700 with self.assertRaises(ZeroDivisionError):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
701 signal.alarm(1)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
702 c.sendall(b"x" * (1024**2))
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
703 if with_timeout:
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
704 signal.signal(signal.SIGALRM, ok_handler)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
705 signal.alarm(1)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
706 self.assertRaises(socket.timeout, c.sendall, b"x" * (1024**2))
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
707 finally:
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
708 signal.signal(signal.SIGALRM, old_alarm)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
709 c.close()
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
710 s.close()
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
711
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
712 def test_sendall_interrupted(self):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
713 self.check_sendall_interrupted(False)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
714
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
715 def test_sendall_interrupted_with_timeout(self):
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
716 self.check_sendall_interrupted(True)
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
717
65707
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
718 def test_dealloc_warn(self):
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
719 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
720 r = repr(sock)
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
721 with self.assertWarns(ResourceWarning) as cm:
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
722 sock = None
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
723 support.gc_collect()
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
724 self.assertIn(r, str(cm.warning.args[0]))
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
725 # An open socket file object gets dereferenced after the socket
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
726 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
727 f = sock.makefile('rb')
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
728 r = repr(sock)
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
729 sock = None
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
730 support.gc_collect()
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
731 with self.assertWarns(ResourceWarning):
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
732 f = None
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
733 support.gc_collect()
cff78ffb932a Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents: 65606
diff changeset
734
64959
aed04b3f0fe3 Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents: 64817
diff changeset
735
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
736 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
737 class BasicTCPTest(SocketConnectedTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
738
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
739 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
740 SocketConnectedTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
741
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
742 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
743 # Testing large receive over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
744 msg = self.cli_conn.recv(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
745 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
746
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
747 def _testRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
748 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
749
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
750 def testOverFlowRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
751 # Testing receive in chunks over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
752 seg1 = self.cli_conn.recv(len(MSG) - 3)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
753 seg2 = self.cli_conn.recv(1024)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
754 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
755 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
756
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
757 def _testOverFlowRecv(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
758 self.serv_conn.send(MSG)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
759
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
760 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
761 # Testing large recvfrom() over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
762 msg, addr = self.cli_conn.recvfrom(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
763 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
764
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
765 def _testRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
766 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
767
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
768 def testOverFlowRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
769 # Testing recvfrom() in chunks over TCP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
770 seg1, addr = self.cli_conn.recvfrom(len(MSG)-3)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
771 seg2, addr = self.cli_conn.recvfrom(1024)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
772 msg = seg1 + seg2
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
773 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
774
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
775 def _testOverFlowRecvFrom(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
776 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
777
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
778 def testSendAll(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
779 # Testing sendall() with a 2048 byte string over TCP
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
780 msg = b''
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
781 while 1:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
782 read = self.cli_conn.recv(1024)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
783 if not read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
784 break
24675
fec0b0aef14b testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents: 24674
diff changeset
785 msg += read
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
786 self.assertEqual(msg, b'f' * 2048)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
787
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
788 def _testSendAll(self):
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
789 big_chunk = b'f' * 2048
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
790 self.serv_conn.sendall(big_chunk)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
791
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
792 def testFromFd(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
793 # Testing fromfd()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
794 fd = self.cli_conn.fileno()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
795 sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
796 self.addCleanup(sock.close)
64747
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64593
diff changeset
797 self.assertIsInstance(sock, socket.socket)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
798 msg = sock.recv(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
799 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
800
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
801 def _testFromFd(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
802 self.serv_conn.send(MSG)
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
803
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
804 def testDup(self):
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
805 # Testing dup()
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
806 sock = self.cli_conn.dup()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
807 self.addCleanup(sock.close)
43881
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
808 msg = sock.recv(1024)
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
809 self.assertEqual(msg, MSG)
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
810
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
811 def _testDup(self):
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
812 self.serv_conn.send(MSG)
70cfc7b504c2 This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
813
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
814 def testShutdown(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
815 # Testing shutdown()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
816 msg = self.cli_conn.recv(1024)
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
817 self.assertEqual(msg, MSG)
50800
b17ec4c4a7c3 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50741
diff changeset
818 # wait for _testShutdown to finish: on OS X, when the server
b17ec4c4a7c3 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50741
diff changeset
819 # closes the connection the client also becomes disconnected,
b17ec4c4a7c3 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50741
diff changeset
820 # and the client's shutdown call will fail. (Issue #4397.)
b17ec4c4a7c3 Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50741
diff changeset
821 self.done.wait()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
822
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
823 def _testShutdown(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
824 self.serv_conn.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
825 self.serv_conn.shutdown(2)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
826
63890
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
827 def testDetach(self):
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
828 # Testing detach()
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
829 fileno = self.cli_conn.fileno()
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
830 f = self.cli_conn.detach()
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
831 self.assertEqual(f, fileno)
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
832 # cli_conn cannot be used anymore...
63856
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
833 self.assertRaises(socket.error, self.cli_conn.recv, 1024)
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
834 self.cli_conn.close()
63890
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
835 # ...but we can create another socket using the (still open)
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
836 # file descriptor
63856
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
837 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, fileno=f)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
838 self.addCleanup(sock.close)
63856
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
839 msg = sock.recv(1024)
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
840 self.assertEqual(msg, MSG)
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
841
63890
60076f28c023 Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents: 63856
diff changeset
842 def _testDetach(self):
63856
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
843 self.serv_conn.send(MSG)
3f8cef837f07 Issue #8524: Add a forget() method to socket objects, so as to put the
Antoine Pitrou <solipsis@pitrou.net>
parents: 61259
diff changeset
844
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
845 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
846 class BasicUDPTest(ThreadedUDPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
847
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
848 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
849 ThreadedUDPSocketTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
850
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
851 def testSendtoAndRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
852 # Testing sendto() and Recv() over UDP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
853 msg = self.serv.recv(len(MSG))
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
854 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
855
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
856 def _testSendtoAndRecv(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
857 self.cli.sendto(MSG, 0, (HOST, self.port))
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
858
23798
1da6d67f4a05 Some provisional changes to get more tests to run on Windows (I hope).
Guido van Rossum <guido@python.org>
parents: 23797
diff changeset
859 def testRecvFrom(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
860 # Testing recvfrom() over UDP
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
861 msg, addr = self.serv.recvfrom(len(MSG))
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
862 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
863
23798
1da6d67f4a05 Some provisional changes to get more tests to run on Windows (I hope).
Guido van Rossum <guido@python.org>
parents: 23797
diff changeset
864 def _testRecvFrom(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
865 self.cli.sendto(MSG, 0, (HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
866
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
867 def testRecvFromNegative(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
868 # Negative lengths passed to recvfrom should give ValueError.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
869 self.assertRaises(ValueError, self.serv.recvfrom, -1)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
870
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
871 def _testRecvFromNegative(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
872 self.cli.sendto(MSG, 0, (HOST, self.port))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
873
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
874 @unittest.skipUnless(thread, 'Threading required for this test.')
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
875 class TCPCloserTest(ThreadedTCPSocketTest):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
876
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
877 def testClose(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
878 conn, addr = self.serv.accept()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
879 conn.close()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
880
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
881 sd = self.cli
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
882 read, write, err = select.select([sd], [], [], 1.0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
883 self.assertEqual(read, [sd])
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
884 self.assertEqual(sd.recv(1), b'')
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
885
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
886 # Calling close() many times should be safe.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
887 conn.close()
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
888 conn.close()
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
889
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
890 def _testClose(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
891 self.cli.connect((HOST, self.port))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
892 time.sleep(1.0)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
893
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
894 @unittest.skipUnless(thread, 'Threading required for this test.')
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
895 class BasicSocketPairTest(SocketPairTest):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
896
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
897 def __init__(self, methodName='runTest'):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
898 SocketPairTest.__init__(self, methodName=methodName)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
899
64754
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
900 def _check_defaults(self, sock):
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
901 self.assertIsInstance(sock, socket.socket)
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
902 if hasattr(socket, 'AF_UNIX'):
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
903 self.assertEqual(sock.family, socket.AF_UNIX)
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
904 else:
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
905 self.assertEqual(sock.family, socket.AF_INET)
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
906 self.assertEqual(sock.type, socket.SOCK_STREAM)
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
907 self.assertEqual(sock.proto, 0)
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
908
64747
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64593
diff changeset
909 def _testDefaults(self):
64754
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
910 self._check_defaults(self.cli)
64747
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64593
diff changeset
911
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64593
diff changeset
912 def testDefaults(self):
64754
b81e8e3fa5bb Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents: 64747
diff changeset
913 self._check_defaults(self.serv)
64747
534853ba8cd3 Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents: 64593
diff changeset
914
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
915 def testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
916 msg = self.serv.recv(1024)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
917 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
918
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
919 def _testRecv(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
920 self.cli.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
921
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
922 def testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
923 self.serv.send(MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
924
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
925 def _testSend(self):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
926 msg = self.cli.recv(1024)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
927 self.assertEqual(msg, MSG)
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
928
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
929 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
930 class NonBlockingTCPTests(ThreadedTCPSocketTest):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
931
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
932 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
933 ThreadedTCPSocketTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
934
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
935 def testSetBlocking(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
936 # Testing whether set blocking works
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
937 self.serv.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
938 start = time.time()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
939 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
940 self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
941 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
942 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
943 end = time.time()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
944 self.assertTrue((end - start) < 1.0, "Error setting non-blocking mode.")
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
945
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
946 def _testSetBlocking(self):
17208
c8817a9bbbdd Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents: 16519
diff changeset
947 pass
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
948
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
949 if hasattr(socket, "SOCK_NONBLOCK"):
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
950 def testInitNonBlocking(self):
65606
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
951 v = linux_version()
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
952 if v < (2, 6, 28):
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
953 self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
954 % ".".join(map(str, v)))
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
955 # reinit server socket
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
956 self.serv.close()
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
957 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM |
65606
a30e75820b8d Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents: 65604
diff changeset
958 socket.SOCK_NONBLOCK)
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
959 self.port = support.bind_port(self.serv)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
960 self.serv.listen(1)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
961 # actual testing
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
962 start = time.time()
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
963 try:
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
964 self.serv.accept()
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
965 except socket.error:
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
966 pass
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
967 end = time.time()
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
968 self.assertTrue((end - start) < 1.0, "Error creating with non-blocking mode.")
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
969
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
970 def _testInitNonBlocking(self):
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
971 pass
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
972
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
973 def testAccept(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
974 # Testing non-blocking accept
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
975 self.serv.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
976 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
977 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
978 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
979 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
980 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
981 self.fail("Error trying to do non-blocking accept.")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
982 read, write, err = select.select([self.serv], [], [])
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
983 if self.serv in read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
984 conn, addr = self.serv.accept()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
985 conn.close()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
986 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
987 self.fail("Error trying to do accept after select.")
20853
77bb2ae067c2 Correct getnameinfo refcounting and tuple parsing. Fixes #476648.
Martin v. Löwis <martin@v.loewis.de>
parents: 20733
diff changeset
988
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
989 def _testAccept(self):
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
990 time.sleep(0.1)
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
991 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
992
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
993 def testConnect(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
994 # Testing non-blocking connect
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
995 conn, addr = self.serv.accept()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
996 conn.close()
4880
9a7e9f10aed6 Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff changeset
997
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
998 def _testConnect(self):
23818
8a0a14e3725e Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents: 23815
diff changeset
999 self.cli.settimeout(10)
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1000 self.cli.connect((HOST, self.port))
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1001
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1002 def testRecv(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1003 # Testing non-blocking recv
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1004 conn, addr = self.serv.accept()
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1005 conn.setblocking(0)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1006 try:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1007 msg = conn.recv(len(MSG))
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1008 except socket.error:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1009 pass
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1010 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1011 self.fail("Error trying to do non-blocking recv.")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1012 read, write, err = select.select([conn], [], [])
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1013 if conn in read:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1014 msg = conn.recv(len(MSG))
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1015 conn.close()
23794
1f1a6f7dd600 Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents: 23790
diff changeset
1016 self.assertEqual(msg, MSG)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1017 else:
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1018 self.fail("Error during select call to non-blocking socket.")
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1019
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1020 def _testRecv(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1021 self.cli.connect((HOST, self.port))
24318
12815d05080c Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents: 24293
diff changeset
1022 time.sleep(0.1)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1023 self.cli.send(MSG)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1024
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
1025 @unittest.skipUnless(thread, 'Threading required for this test.')
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1026 class FileObjectClassTestCase(SocketConnectedTest):
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1027 """Unit tests for the object returned by socket.makefile()
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1028
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1029 self.read_file is the io object returned by makefile() on
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1030 the client connection. You can read from this file to
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1031 get output from the server.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1032
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1033 self.write_file is the io object returned by makefile() on the
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1034 server connection. You can write to this file to send output
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1035 to the client.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1036 """
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
1037
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1038 bufsize = -1 # Use default buffer size
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1039 encoding = 'utf8'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1040 errors = 'strict'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1041 newline = None
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1042
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1043 read_mode = 'rb'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1044 read_msg = MSG
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1045 write_mode = 'wb'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1046 write_msg = MSG
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1047
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1048 def __init__(self, methodName='runTest'):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1049 SocketConnectedTest.__init__(self, methodName=methodName)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1050
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1051 def setUp(self):
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1052 self.evt1, self.evt2, self.serv_finished, self.cli_finished = [
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1053 threading.Event() for i in range(4)]
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1054 SocketConnectedTest.setUp(self)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1055 self.read_file = self.cli_conn.makefile(
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1056 self.read_mode, self.bufsize,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1057 encoding = self.encoding,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1058 errors = self.errors,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1059 newline = self.newline)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1060
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1061 def tearDown(self):
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1062 self.serv_finished.set()
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1063 self.read_file.close()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1064 self.assertTrue(self.read_file.closed)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1065 self.read_file = None
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1066 SocketConnectedTest.tearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1067
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1068 def clientSetUp(self):
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1069 SocketConnectedTest.clientSetUp(self)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1070 self.write_file = self.serv_conn.makefile(
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1071 self.write_mode, self.bufsize,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1072 encoding = self.encoding,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1073 errors = self.errors,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1074 newline = self.newline)
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
1075
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1076 def clientTearDown(self):
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1077 self.cli_finished.set()
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1078 self.write_file.close()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1079 self.assertTrue(self.write_file.closed)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1080 self.write_file = None
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1081 SocketConnectedTest.clientTearDown(self)
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1082
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1083 def testSmallRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1084 # Performing small file read test
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1085 first_seg = self.read_file.read(len(self.read_msg)-3)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1086 second_seg = self.read_file.read(3)
23800
bf1555a92711 Remove some overly complicated ways to concatenate and repeat strings
Guido van Rossum <guido@python.org>
parents: 23799
diff changeset
1087 msg = first_seg + second_seg
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1088 self.assertEqual(msg, self.read_msg)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1089
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1090 def _testSmallRead(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1091 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1092 self.write_file.flush()
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
1093
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1094 def testFullRead(self):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1095 # read until EOF
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1096 msg = self.read_file.read()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1097 self.assertEqual(msg, self.read_msg)
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1098
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1099 def _testFullRead(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1100 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1101 self.write_file.close()
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1102
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1103 def testUnbufferedRead(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1104 # Performing unbuffered file read test
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1105 buf = type(self.read_msg)()
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1106 while 1:
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1107 char = self.read_file.read(1)
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1108 if not char:
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1109 break
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1110 buf += char
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1111 self.assertEqual(buf, self.read_msg)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1112
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1113 def _testUnbufferedRead(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1114 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1115 self.write_file.flush()
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
1116
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1117 def testReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1118 # Performing file readline test
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1119 line = self.read_file.readline()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1120 self.assertEqual(line, self.read_msg)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1121
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1122 def _testReadline(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1123 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1124 self.write_file.flush()
23715
c2733e8f6612 SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 21315
diff changeset
1125
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1126 def testCloseAfterMakefile(self):
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1127 # The file returned by makefile should keep the socket open.
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1128 self.cli_conn.close()
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1129 # read until EOF
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1130 msg = self.read_file.read()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1131 self.assertEqual(msg, self.read_msg)
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1132
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1133 def _testCloseAfterMakefile(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1134 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1135 self.write_file.flush()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1136
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1137 def testMakefileAfterMakefileClose(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1138 self.read_file.close()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1139 msg = self.cli_conn.recv(len(MSG))
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1140 if isinstance(self.read_msg, str):
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1141 msg = msg.decode()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1142 self.assertEqual(msg, self.read_msg)
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1143
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1144 def _testMakefileAfterMakefileClose(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1145 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1146 self.write_file.flush()
42434
c35140559f87 Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 42169
diff changeset
1147
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
1148 def testClosedAttr(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1149 self.assertTrue(not self.read_file.closed)
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
1150
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
1151 def _testClosedAttr(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1152 self.assertTrue(not self.write_file.closed)
31833
a910b822b12c SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents: 31503
diff changeset
1153
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48623
diff changeset
1154 def testAttributes(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1155 self.assertEqual(self.read_file.mode, self.read_mode)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1156 self.assertEqual(self.read_file.name, self.cli_conn.fileno())
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48623
diff changeset
1157
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48623
diff changeset
1158 def _testAttributes(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1159 self.assertEqual(self.write_file.mode, self.write_mode)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1160 self.assertEqual(self.write_file.name, self.serv_conn.fileno())
49782
5ffd67df8c9f Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48623
diff changeset
1161
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1162 def testRealClose(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1163 self.read_file.close()
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1164 self.assertRaises(ValueError, self.read_file.fileno)
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1165 self.cli_conn.close()
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1166 self.assertRaises(socket.error, self.cli_conn.getsockname)
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1167
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1168 def _testRealClose(self):
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1169 pass
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1170
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1171
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1172 class FileObjectInterruptedTestCase(unittest.TestCase):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1173 """Test that the file object correctly handles EINTR internally."""
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1174
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1175 class MockSocket(object):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1176 def __init__(self, recv_funcs=()):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1177 # A generator that returns callables that we'll call for each
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1178 # call to recv().
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1179 self._recv_step = iter(recv_funcs)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1180
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1181 def recv_into(self, buffer):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1182 data = next(self._recv_step)()
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1183 assert len(buffer) >= len(data)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1184 buffer[:len(data)] = data
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1185 return len(data)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1186
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1187 def _decref_socketios(self):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1188 pass
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1189
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1190 def _textiowrap_for_test(self, buffering=-1):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1191 raw = socket.SocketIO(self, "r")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1192 if buffering < 0:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1193 buffering = io.DEFAULT_BUFFER_SIZE
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1194 if buffering == 0:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1195 return raw
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1196 buffer = io.BufferedReader(raw, buffering)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1197 text = io.TextIOWrapper(buffer, None, None)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1198 text.mode = "rb"
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1199 return text
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1200
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1201 @staticmethod
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1202 def _raise_eintr():
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1203 raise socket.error(errno.EINTR)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1204
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1205 def _textiowrap_mock_socket(self, mock, buffering=-1):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1206 raw = socket.SocketIO(mock, "r")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1207 if buffering < 0:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1208 buffering = io.DEFAULT_BUFFER_SIZE
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1209 if buffering == 0:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1210 return raw
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1211 buffer = io.BufferedReader(raw, buffering)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1212 text = io.TextIOWrapper(buffer, None, None)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1213 text.mode = "rb"
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1214 return text
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1215
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1216 def _test_readline(self, size=-1, buffering=-1):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1217 mock_sock = self.MockSocket(recv_funcs=[
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1218 lambda : b"This is the first line\nAnd the sec",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1219 self._raise_eintr,
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1220 lambda : b"ond line is here\n",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1221 lambda : b"",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1222 lambda : b"", # XXX(gps): io library does an extra EOF read
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1223 ])
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1224 fo = mock_sock._textiowrap_for_test(buffering=buffering)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1225 self.assertEquals(fo.readline(size), "This is the first line\n")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1226 self.assertEquals(fo.readline(size), "And the second line is here\n")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1227
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1228 def _test_read(self, size=-1, buffering=-1):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1229 mock_sock = self.MockSocket(recv_funcs=[
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1230 lambda : b"This is the first line\nAnd the sec",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1231 self._raise_eintr,
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1232 lambda : b"ond line is here\n",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1233 lambda : b"",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1234 lambda : b"", # XXX(gps): io library does an extra EOF read
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1235 ])
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1236 expecting = (b"This is the first line\n"
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1237 b"And the second line is here\n")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1238 fo = mock_sock._textiowrap_for_test(buffering=buffering)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1239 if buffering == 0:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1240 data = b''
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1241 else:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1242 data = ''
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1243 expecting = expecting.decode('utf8')
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1244 while len(data) != len(expecting):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1245 part = fo.read(size)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1246 if not part:
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1247 break
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1248 data += part
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1249 self.assertEquals(data, expecting)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1250
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1251 def test_default(self):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1252 self._test_readline()
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1253 self._test_readline(size=100)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1254 self._test_read()
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1255 self._test_read(size=100)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1256
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1257 def test_with_1k_buffer(self):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1258 self._test_readline(buffering=1024)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1259 self._test_readline(size=100, buffering=1024)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1260 self._test_read(buffering=1024)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1261 self._test_read(size=100, buffering=1024)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1262
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1263 def _test_readline_no_buffer(self, size=-1):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1264 mock_sock = self.MockSocket(recv_funcs=[
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1265 lambda : b"a",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1266 lambda : b"\n",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1267 lambda : b"B",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1268 self._raise_eintr,
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1269 lambda : b"b",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1270 lambda : b"",
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1271 ])
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1272 fo = mock_sock._textiowrap_for_test(buffering=0)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1273 self.assertEquals(fo.readline(size), b"a\n")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1274 self.assertEquals(fo.readline(size), b"Bb")
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1275
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1276 def test_no_buffer(self):
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1277 self._test_readline_no_buffer()
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1278 self._test_readline_no_buffer(size=4)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1279 self._test_read(buffering=0)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1280 self._test_read(size=100, buffering=0)
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1281
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1282
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1283 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase):
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1284
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1285 """Repeat the tests from FileObjectClassTestCase with bufsize==0.
24674
3269bdd65988 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 24655
diff changeset
1286
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1287 In this case (and in this case only), it should be possible to
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1288 create a file object, read a line from it, create another file
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1289 object, read another line from it, without loss of data in the
47318
e1fcc3881cb6 Create http package. #2883.
Georg Brandl <georg@python.org>
parents: 47266
diff changeset
1290 first file object's buffer. Note that http.client relies on this
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1291 when reading multiple requests from the same socket."""
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1292
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1293 bufsize = 0 # Use unbuffered mode
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1294
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1295 def testUnbufferedReadline(self):
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1296 # Read a line, create a new file object, read another line with it
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1297 line = self.read_file.readline() # first line
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1298 self.assertEqual(line, b"A. " + self.write_msg) # first line
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1299 self.read_file = self.cli_conn.makefile('rb', 0)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1300 line = self.read_file.readline() # second line
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1301 self.assertEqual(line, b"B. " + self.write_msg) # second line
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1302
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1303 def _testUnbufferedReadline(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1304 self.write_file.write(b"A. " + self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1305 self.write_file.write(b"B. " + self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1306 self.write_file.flush()
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1307
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1308 def testMakefileClose(self):
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1309 # The file returned by makefile should keep the socket open...
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1310 self.cli_conn.close()
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1311 msg = self.cli_conn.recv(1024)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1312 self.assertEqual(msg, self.read_msg)
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1313 # ...until the file is itself closed
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1314 self.read_file.close()
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1315 self.assertRaises(socket.error, self.cli_conn.recv, 1024)
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1316
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1317 def _testMakefileClose(self):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1318 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1319 self.write_file.flush()
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1320
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1321 def testMakefileCloseSocketDestroy(self):
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1322 refcount_before = sys.getrefcount(self.cli_conn)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1323 self.read_file.close()
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1324 refcount_after = sys.getrefcount(self.cli_conn)
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1325 self.assertEqual(refcount_before - 1, refcount_after)
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1326
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1327 def _testMakefileCloseSocketDestroy(self):
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1328 pass
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1329
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1330 # Non-blocking ops
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1331 # NOTE: to set `read_file` as non-blocking, we must call
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1332 # `cli_conn.setblocking` and vice-versa (see setUp / clientSetUp).
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1333
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1334 def testSmallReadNonBlocking(self):
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1335 self.cli_conn.setblocking(False)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1336 self.assertEqual(self.read_file.readinto(bytearray(10)), None)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1337 self.assertEqual(self.read_file.read(len(self.read_msg) - 3), None)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1338 self.evt1.set()
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1339 self.evt2.wait(1.0)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1340 first_seg = self.read_file.read(len(self.read_msg) - 3)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1341 buf = bytearray(10)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1342 n = self.read_file.readinto(buf)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1343 self.assertEqual(n, 3)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1344 msg = first_seg + buf[:n]
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1345 self.assertEqual(msg, self.read_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1346 self.assertEqual(self.read_file.readinto(bytearray(16)), None)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1347 self.assertEqual(self.read_file.read(1), None)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1348
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1349 def _testSmallReadNonBlocking(self):
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1350 self.evt1.wait(1.0)
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1351 self.write_file.write(self.write_msg)
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1352 self.write_file.flush()
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1353 self.evt2.set()
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1354 # Avoid cloding the socket before the server test has finished,
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1355 # otherwise system recv() will return 0 instead of EWOULDBLOCK.
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1356 self.serv_finished.wait(5.0)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1357
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1358 def testWriteNonBlocking(self):
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1359 self.cli_finished.wait(5.0)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1360 # The client thread can't skip directly - the SkipTest exception
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1361 # would appear as a failure.
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1362 if self.serv_skipped:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1363 self.skipTest(self.serv_skipped)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1364
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1365 def _testWriteNonBlocking(self):
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1366 self.serv_skipped = None
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1367 self.serv_conn.setblocking(False)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1368 # Try to saturate the socket buffer pipe with repeated large writes.
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1369 BIG = b"x" * (1024 ** 2)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1370 LIMIT = 10
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1371 # The first write() succeeds since a chunk of data can be buffered
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1372 n = self.write_file.write(BIG)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1373 self.assertGreater(n, 0)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1374 for i in range(LIMIT):
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1375 n = self.write_file.write(BIG)
64817
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1376 if n is None:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1377 # Succeeded
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1378 break
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1379 self.assertGreater(n, 0)
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1380 else:
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1381 # Let us know that this test didn't manage to establish
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1382 # the expected conditions. This is not a failure in itself but,
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1383 # if it happens repeatedly, the test should be fixed.
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1384 self.serv_skipped = "failed to saturate the socket buffer"
f3dcb314c3e3 Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents: 64754
diff changeset
1385
50741
5f4c2de95091 Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents: 49800
diff changeset
1386
24655
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1387 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1388
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1389 bufsize = 1 # Default-buffered for reading; line-buffered for writing
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1390
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1391
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1392 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase):
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1393
18ca4e783f98 Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents: 24648
diff changeset
1394 bufsize = 2 # Exercise the buffering code
24634
778093533a46 "Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents: 24485
diff changeset
1395
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1396
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1397 class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase):
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1398 """Tests for socket.makefile() in text mode (rather than binary)"""
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1399
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1400 read_mode = 'r'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1401 read_msg = MSG.decode('utf8')
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1402 write_mode = 'wb'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1403 write_msg = MSG
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1404 newline = ''
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1405
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1406
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1407 class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase):
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1408 """Tests for socket.makefile() in text mode (rather than binary)"""
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1409
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1410 read_mode = 'rb'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1411 read_msg = MSG
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1412 write_mode = 'w'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1413 write_msg = MSG.decode('utf8')
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1414 newline = ''
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1415
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1416
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1417 class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase):
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1418 """Tests for socket.makefile() in text mode (rather than binary)"""
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1419
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1420 read_mode = 'r'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1421 read_msg = MSG.decode('utf8')
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1422 write_mode = 'w'
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1423 write_msg = MSG.decode('utf8')
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1424 newline = ''
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1425
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1426
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1427 class NetworkConnectionTest(object):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1428 """Prove network connection."""
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
1429
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1430 def clientSetUp(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1431 # We're inherited below by BasicTCPTest2, which also inherits
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1432 # BasicTCPTest, which defines self.port referenced below.
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1433 self.cli = socket.create_connection((HOST, self.port))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1434 self.serv_conn = self.cli
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1435
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1436 class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1437 """Tests that NetworkConnection does not break existing TCP functionality.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1438 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1439
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1440 class NetworkConnectionNoServer(unittest.TestCase):
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
1441
64534
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1442 class MockSocket(socket.socket):
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1443 def connect(self, *args):
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1444 raise socket.timeout('timed out')
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1445
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1446 @contextlib.contextmanager
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1447 def mocked_socket_module(self):
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1448 """Return a socket which times out on connect"""
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1449 old_socket = socket.socket
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1450 socket.socket = self.MockSocket
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1451 try:
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1452 yield
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1453 finally:
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1454 socket.socket = old_socket
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1455
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1456 def test_connect(self):
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
1457 port = support.find_unused_port()
64534
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1458 cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1459 self.addCleanup(cli.close)
64534
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1460 with self.assertRaises(socket.error) as cm:
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1461 cli.connect((HOST, port))
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1462 self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1463
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1464 def test_create_connection(self):
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1465 # Issue #9792: errors raised by create_connection() should have
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1466 # a proper errno attribute.
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1467 port = support.find_unused_port()
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1468 with self.assertRaises(socket.error) as cm:
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1469 socket.create_connection((HOST, port))
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1470 self.assertEqual(cm.exception.errno, errno.ECONNREFUSED)
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1471
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1472 def test_create_connection_timeout(self):
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1473 # Issue #9792: create_connection() should not recast timeout errors
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1474 # as generic socket errors.
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1475 with self.mocked_socket_module():
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1476 with self.assertRaises(socket.timeout):
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1477 socket.create_connection((HOST, 1234))
b2f7cf9c6776 Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64442
diff changeset
1478
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1479
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
1480 @unittest.skipUnless(thread, 'Threading required for this test.')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1481 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1482
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1483 def __init__(self, methodName='runTest'):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1484 SocketTCPTest.__init__(self, methodName=methodName)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1485 ThreadableTest.__init__(self)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1486
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1487 def clientSetUp(self):
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56367
diff changeset
1488 self.source_port = support.find_unused_port()
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1489
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1490 def clientTearDown(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1491 self.cli.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1492 self.cli = None
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1493 ThreadableTest.clientTearDown(self)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1494
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1495 def _justAccept(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1496 conn, addr = self.serv.accept()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1497 conn.close()
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1498
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1499 testFamily = _justAccept
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1500 def _testFamily(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1501 self.cli = socket.create_connection((HOST, self.port), timeout=30)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1502 self.addCleanup(self.cli.close)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1503 self.assertEqual(self.cli.family, 2)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1504
58136
ecc8a45894c9 Merged revisions 77286 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
1505 testSourceAddress = _justAccept
ecc8a45894c9 Merged revisions 77286 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
1506 def _testSourceAddress(self):
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56367
diff changeset
1507 self.cli = socket.create_connection((HOST, self.port), timeout=30,
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56367
diff changeset
1508 source_address=('', self.source_port))
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1509 self.addCleanup(self.cli.close)
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56367
diff changeset
1510 self.assertEqual(self.cli.getsockname()[1], self.source_port)
58136
ecc8a45894c9 Merged revisions 77286 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
1511 # The port number being used is sufficient to show that the bind()
ecc8a45894c9 Merged revisions 77286 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58114
diff changeset
1512 # call happened.
58114
b0386fe650c7 Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 56367
diff changeset
1513
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1514 testTimeoutDefault = _justAccept
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1515 def _testTimeoutDefault(self):
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1516 # passing no explicit timeout uses socket's global default
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1517 self.assertTrue(socket.getdefaulttimeout() is None)
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1518 socket.setdefaulttimeout(42)
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1519 try:
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1520 self.cli = socket.create_connection((HOST, self.port))
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1521 self.addCleanup(self.cli.close)
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1522 finally:
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1523 socket.setdefaulttimeout(None)
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1524 self.assertEquals(self.cli.gettimeout(), 42)
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1525
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1526 testTimeoutNone = _justAccept
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1527 def _testTimeoutNone(self):
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1528 # None timeout means the same as sock.settimeout(None)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1529 self.assertTrue(socket.getdefaulttimeout() is None)
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1530 socket.setdefaulttimeout(30)
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1531 try:
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1532 self.cli = socket.create_connection((HOST, self.port), timeout=None)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1533 self.addCleanup(self.cli.close)
47559
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1534 finally:
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1535 socket.setdefaulttimeout(None)
2925b568aacc Merged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47318
diff changeset
1536 self.assertEqual(self.cli.gettimeout(), None)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1537
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1538 testTimeoutValueNamed = _justAccept
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1539 def _testTimeoutValueNamed(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1540 self.cli = socket.create_connection((HOST, self.port), timeout=30)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1541 self.assertEqual(self.cli.gettimeout(), 30)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1542
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1543 testTimeoutValueNonamed = _justAccept
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1544 def _testTimeoutValueNonamed(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1545 self.cli = socket.create_connection((HOST, self.port), 30)
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1546 self.addCleanup(self.cli.close)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1547 self.assertEqual(self.cli.gettimeout(), 30)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1548
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
1549 @unittest.skipUnless(thread, 'Threading required for this test.')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1550 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1551
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1552 def __init__(self, methodName='runTest'):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1553 SocketTCPTest.__init__(self, methodName=methodName)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1554 ThreadableTest.__init__(self)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1555
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1556 def clientSetUp(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1557 pass
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1558
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1559 def clientTearDown(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1560 self.cli.close()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1561 self.cli = None
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1562 ThreadableTest.clientTearDown(self)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1563
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1564 def testInsideTimeout(self):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1565 conn, addr = self.serv.accept()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1566 self.addCleanup(conn.close)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1567 time.sleep(3)
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
1568 conn.send(b"done!")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1569 testOutsideTimeout = testInsideTimeout
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1570
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1571 def _testInsideTimeout(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1572 self.cli = sock = socket.create_connection((HOST, self.port))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1573 data = sock.recv(5)
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
1574 self.assertEqual(data, b"done!")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1575
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1576 def _testOutsideTimeout(self):
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46262
diff changeset
1577 self.cli = sock = socket.create_connection((HOST, self.port), timeout=1)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1578 self.assertRaises(socket.timeout, lambda: sock.recv(5))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1579
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1580
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1581 class TCPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1582
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1583 def testTCPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1584 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1585 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1586 self.serv.accept()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1587 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1588 "Error generating a timeout exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1589
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1590 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1591 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1592 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1593 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1594 foo = self.serv.accept()
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1595 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1596 self.fail("caught timeout instead of error (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1597 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1598 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1599 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1600 self.fail("caught unexpected exception (TCP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1601 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1602 self.fail("accept() returned success when we did not expect it")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1603
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1604 def testInterruptedTimeout(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1605 # XXX I don't know how to do this test on MSWindows or any other
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1606 # plaform that doesn't support signal.alarm() or os.kill(), though
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1607 # the bug should have existed on all platforms.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1608 if not hasattr(signal, "alarm"):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1609 return # can only test on *nix
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1610 self.serv.settimeout(5.0) # must be longer than alarm
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1611 class Alarm(Exception):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1612 pass
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1613 def alarm_handler(signal, frame):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1614 raise Alarm
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1615 old_alarm = signal.signal(signal.SIGALRM, alarm_handler)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1616 try:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1617 signal.alarm(2) # POSIX allows alarm to be up to 1 second early
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1618 try:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1619 foo = self.serv.accept()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1620 except socket.timeout:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1621 self.fail("caught timeout instead of Alarm")
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1622 except Alarm:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1623 pass
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1624 except:
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44782
diff changeset
1625 self.fail("caught other exception instead of Alarm:"
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44782
diff changeset
1626 " %s(%s):\n%s" %
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44782
diff changeset
1627 (sys.exc_info()[:2] + (traceback.format_exc(),)))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1628 else:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1629 self.fail("nothing caught")
46161
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44782
diff changeset
1630 finally:
848160d10780 Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44782
diff changeset
1631 signal.alarm(0) # shut off alarm
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1632 except Alarm:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1633 self.fail("got Alarm in wrong place")
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1634 finally:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1635 # no alarm can be pending. Safe to restore old handler.
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1636 signal.signal(signal.SIGALRM, old_alarm)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1637
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1638 class UDPTimeoutTest(SocketTCPTest):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1639
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1640 def testUDPTimeout(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1641 def raise_timeout(*args, **kwargs):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1642 self.serv.settimeout(1.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1643 self.serv.recv(1024)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1644 self.assertRaises(socket.timeout, raise_timeout,
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1645 "Error generating a timeout exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1646
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1647 def testTimeoutZero(self):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1648 ok = False
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1649 try:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1650 self.serv.settimeout(0.0)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1651 foo = self.serv.recv(1024)
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1652 except socket.timeout:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1653 self.fail("caught timeout instead of error (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1654 except socket.error:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1655 ok = True
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1656 except:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1657 self.fail("caught unexpected exception (UDP)")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1658 if not ok:
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1659 self.fail("recv() returned success when we did not expect it")
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1660
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1661 class TestExceptions(unittest.TestCase):
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1663 def testExceptionTree(self):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1664 self.assertTrue(issubclass(socket.error, Exception))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1665 self.assertTrue(issubclass(socket.herror, socket.error))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1666 self.assertTrue(issubclass(socket.gaierror, socket.error))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 51579
diff changeset
1667 self.assertTrue(issubclass(socket.timeout, socket.error))
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1668
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1669 class TestLinuxAbstractNamespace(unittest.TestCase):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1670
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1671 UNIX_PATH_MAX = 108
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1672
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1673 def testLinuxAbstractNamespace(self):
42596
980308fbda29 Down with str8!
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
1674 address = b"\x00python-test-hello\x00\xff"
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1675 s1 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1676 s1.bind(address)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1677 s1.listen(1)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1678 s2 = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1679 s2.connect(s1.getsockname())
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1680 s1.accept()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1681 self.assertEqual(s1.getsockname(), address)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1682 self.assertEqual(s2.getpeername(), address)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1683
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1684 def testMaxName(self):
42596
980308fbda29 Down with str8!
Guido van Rossum <guido@python.org>
parents: 42434
diff changeset
1685 address = b"\x00" + b"h" * (self.UNIX_PATH_MAX - 1)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1686 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1687 s.bind(address)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1688 self.assertEqual(s.getsockname(), address)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1689
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1690 def testNameOverflow(self):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1691 address = "\x00" + "h" * self.UNIX_PATH_MAX
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1692 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1693 self.assertRaises(socket.error, s.bind, address)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1694
29662
1308d7ec9953 SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents: 29087
diff changeset
1695
61073
493bb4585e38 Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59677
diff changeset
1696 @unittest.skipUnless(thread, 'Threading required for this test.')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1697 class BufferIOTest(SocketConnectedTest):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1698 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1699 Test the buffer versions of socket.recv() and socket.send().
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1700 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1701 def __init__(self, methodName='runTest'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1702 SocketConnectedTest.__init__(self, methodName=methodName)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1703
59677
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1704 def testRecvIntoArray(self):
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1705 buf = bytearray(1024)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1706 nbytes = self.cli_conn.recv_into(buf)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1707 self.assertEqual(nbytes, len(MSG))
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1708 msg = buf[:len(MSG)]
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1709 self.assertEqual(msg, MSG)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1710
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1711 def _testRecvIntoArray(self):
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1712 buf = bytes(MSG)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1713 self.serv_conn.send(buf)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1714
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1715 def testRecvIntoBytearray(self):
48623
bf50085fbcd3 #2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 47615
diff changeset
1716 buf = bytearray(1024)
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1717 nbytes = self.cli_conn.recv_into(buf)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1718 self.assertEqual(nbytes, len(MSG))
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
1719 msg = buf[:len(MSG)]
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1720 self.assertEqual(msg, MSG)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1721
59677
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1722 _testRecvIntoBytearray = _testRecvIntoArray
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1723
59677
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1724 def testRecvIntoMemoryview(self):
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1725 buf = bytearray(1024)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1726 nbytes = self.cli_conn.recv_into(memoryview(buf))
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1727 self.assertEqual(nbytes, len(MSG))
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1728 msg = buf[:len(MSG)]
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1729 self.assertEqual(msg, MSG)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1730
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1731 _testRecvIntoMemoryview = _testRecvIntoArray
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1732
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1733 def testRecvFromIntoArray(self):
48623
bf50085fbcd3 #2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents: 47615
diff changeset
1734 buf = bytearray(1024)
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1735 nbytes, addr = self.cli_conn.recvfrom_into(buf)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1736 self.assertEqual(nbytes, len(MSG))
41809
8e062e572ea4 Sockets facelift. APIs that could return binary data (e.g. aton() and
Guido van Rossum <guido@python.org>
parents: 41770
diff changeset
1737 msg = buf[:len(MSG)]
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1738 self.assertEqual(msg, MSG)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1739
59677
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1740 def _testRecvFromIntoArray(self):
41770
651fa2c9c2ab Make test_socket pass. There was an unchecked error when a Unicode
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
1741 buf = bytes(MSG)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1742 self.serv_conn.send(buf)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1743
59677
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1744 def testRecvFromIntoBytearray(self):
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1745 buf = bytearray(1024)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1746 nbytes, addr = self.cli_conn.recvfrom_into(buf)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1747 self.assertEqual(nbytes, len(MSG))
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1748 msg = buf[:len(MSG)]
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1749 self.assertEqual(msg, MSG)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1750
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1751 _testRecvFromIntoBytearray = _testRecvFromIntoArray
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1752
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1753 def testRecvFromIntoMemoryview(self):
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1754 buf = bytearray(1024)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1755 nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf))
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1756 self.assertEqual(nbytes, len(MSG))
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1757 msg = buf[:len(MSG)]
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1758 self.assertEqual(msg, MSG)
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1759
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1760 _testRecvFromIntoMemoryview = _testRecvFromIntoArray
d62ca1eaccb9 NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents: 59522
diff changeset
1761
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1762
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1763 TIPC_STYPE = 2000
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1764 TIPC_LOWER = 200
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1765 TIPC_UPPER = 210
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1766
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1767 def isTipcAvailable():
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1768 """Check if the TIPC module is loaded
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1769
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1770 The TIPC module is not loaded automatically on Ubuntu and probably
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1771 other Linux distros.
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1772 """
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1773 if not hasattr(socket, "AF_TIPC"):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1774 return False
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1775 if not os.path.isfile("/proc/modules"):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1776 return False
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1777 with open("/proc/modules") as f:
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1778 for line in f:
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1779 if line.startswith("tipc "):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1780 return True
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
1781 if support.verbose:
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1782 print("TIPC module is not loaded, please 'sudo modprobe tipc'")
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1783 return False
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1784
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1785 class TIPCTest (unittest.TestCase):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1786 def testRDM(self):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1787 srv = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1788 cli = socket.socket(socket.AF_TIPC, socket.SOCK_RDM)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1789
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1790 srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1791 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1792 TIPC_LOWER, TIPC_UPPER)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1793 srv.bind(srvaddr)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1794
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1795 sendaddr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1796 TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1797 cli.sendto(MSG, sendaddr)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1798
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1799 msg, recvaddr = srv.recvfrom(1024)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1800
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1801 self.assertEqual(cli.getsockname(), recvaddr)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1802 self.assertEqual(msg, MSG)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1803
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1804
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1805 class TIPCThreadableTest (unittest.TestCase, ThreadableTest):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1806 def __init__(self, methodName = 'runTest'):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1807 unittest.TestCase.__init__(self, methodName = methodName)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1808 ThreadableTest.__init__(self)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1809
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1810 def setUp(self):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1811 self.srv = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1812 self.srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1813 srvaddr = (socket.TIPC_ADDR_NAMESEQ, TIPC_STYPE,
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1814 TIPC_LOWER, TIPC_UPPER)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1815 self.srv.bind(srvaddr)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1816 self.srv.listen(5)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1817 self.serverExplicitReady()
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1818 self.conn, self.connaddr = self.srv.accept()
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1819
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1820 def clientSetUp(self):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1821 # The is a hittable race between serverExplicitReady() and the
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1822 # accept() call; sleep a little while to avoid it, otherwise
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1823 # we could get an exception
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1824 time.sleep(0.1)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1825 self.cli = socket.socket(socket.AF_TIPC, socket.SOCK_STREAM)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1826 addr = (socket.TIPC_ADDR_NAME, TIPC_STYPE,
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1827 TIPC_LOWER + int((TIPC_UPPER - TIPC_LOWER) / 2), 0)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1828 self.cli.connect(addr)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1829 self.cliaddr = self.cli.getsockname()
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1830
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1831 def testStream(self):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1832 msg = self.conn.recv(1024)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1833 self.assertEqual(msg, MSG)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1834 self.assertEqual(self.cliaddr, self.connaddr)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1835
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1836 def _testStream(self):
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1837 self.cli.send(MSG)
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1838 self.cli.close()
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1839
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1840
64574
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1841 @unittest.skipUnless(thread, 'Threading required for this test.')
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1842 class ContextManagersTest(ThreadedTCPSocketTest):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1843
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1844 def _testSocketClass(self):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1845 # base test
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1846 with socket.socket() as sock:
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1847 self.assertFalse(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1848 self.assertTrue(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1849 # close inside with block
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1850 with socket.socket() as sock:
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1851 sock.close()
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1852 self.assertTrue(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1853 # exception inside with block
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1854 with socket.socket() as sock:
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1855 self.assertRaises(socket.error, sock.sendall, b'foo')
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1856 self.assertTrue(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1857
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1858 def testCreateConnectionBase(self):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1859 conn, addr = self.serv.accept()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1860 self.addCleanup(conn.close)
64574
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1861 data = conn.recv(1024)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1862 conn.sendall(data)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1863
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1864 def _testCreateConnectionBase(self):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1865 address = self.serv.getsockname()
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1866 with socket.create_connection(address) as sock:
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1867 self.assertFalse(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1868 sock.sendall(b'foo')
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1869 self.assertEqual(sock.recv(1024), b'foo')
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1870 self.assertTrue(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1871
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1872 def testCreateConnectionClose(self):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1873 conn, addr = self.serv.accept()
65882
889fd8ecbff4 Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents: 65707
diff changeset
1874 self.addCleanup(conn.close)
64574
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1875 data = conn.recv(1024)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1876 conn.sendall(data)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1877
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1878 def _testCreateConnectionClose(self):
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1879 address = self.serv.getsockname()
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1880 with socket.create_connection(address) as sock:
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1881 sock.close()
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1882 self.assertTrue(sock._closed)
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1883 self.assertRaises(socket.error, sock.sendall, b'foo')
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1884
65604
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1885
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1886 @unittest.skipUnless(hasattr(socket, "SOCK_CLOEXEC"),
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1887 "SOCK_CLOEXEC not defined")
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1888 @unittest.skipUnless(fcntl, "module fcntl not available")
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1889 class CloexecConstantTest(unittest.TestCase):
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1890 def test_SOCK_CLOEXEC(self):
65604
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1891 v = linux_version()
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1892 if v < (2, 6, 28):
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1893 self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1894 % ".".join(map(str, v)))
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1895 s = socket.socket(socket.AF_INET,
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1896 socket.SOCK_STREAM | socket.SOCK_CLOEXEC)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1897 self.assertTrue(s.type & socket.SOCK_CLOEXEC)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1898 self.assertTrue(fcntl.fcntl(s, fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1899
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1900
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1901 @unittest.skipUnless(hasattr(socket, "SOCK_NONBLOCK"),
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1902 "SOCK_NONBLOCK not defined")
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1903 class NonblockConstantTest(unittest.TestCase):
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1904 def checkNonblock(self, s, nonblock=True, timeout=0.0):
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1905 if nonblock:
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1906 self.assertTrue(s.type & socket.SOCK_NONBLOCK)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1907 self.assertEqual(s.gettimeout(), timeout)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1908 else:
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1909 self.assertFalse(s.type & socket.SOCK_NONBLOCK)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1910 self.assertEqual(s.gettimeout(), None)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1911
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1912 def test_SOCK_NONBLOCK(self):
65604
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1913 v = linux_version()
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1914 if v < (2, 6, 28):
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1915 self.skipTest("Linux kernel 2.6.28 or higher required, not %s"
8d7f18332db7 Fix SOCK_CLOEXEC and SOCK_NONBLOCK tests on recent glibcs with old Linux kernels.
Antoine Pitrou <solipsis@pitrou.net>
parents: 65416
diff changeset
1916 % ".".join(map(str, v)))
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1917 # a lot of it seems silly and redundant, but I wanted to test that
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1918 # changing back and forth worked ok
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1919 s = socket.socket(socket.AF_INET,
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1920 socket.SOCK_STREAM | socket.SOCK_NONBLOCK)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1921 self.checkNonblock(s)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1922 s.setblocking(1)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1923 self.checkNonblock(s, False)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1924 s.setblocking(0)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1925 self.checkNonblock(s)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1926 s.settimeout(None)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1927 self.checkNonblock(s, False)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1928 s.settimeout(2.0)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1929 self.checkNonblock(s, timeout=2.0)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1930 s.setblocking(1)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1931 self.checkNonblock(s, False)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1932 # defaulttimeout
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1933 t = socket.getdefaulttimeout()
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1934 socket.setdefaulttimeout(0.0)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1935 self.checkNonblock(socket.socket())
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1936 socket.setdefaulttimeout(None)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1937 self.checkNonblock(socket.socket(), False)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1938 socket.setdefaulttimeout(2.0)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1939 self.checkNonblock(socket.socket(), timeout=2.0)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1940 socket.setdefaulttimeout(None)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1941 self.checkNonblock(socket.socket(), False)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1942 socket.setdefaulttimeout(t)
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1943
64574
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1944
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
1945 def test_main():
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1946 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest,
61259
1f7506f23771 Remove traces of MacOS9 support.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 61073
diff changeset
1947 TestExceptions, BufferIOTest, BasicTCPTest2, BasicUDPTest, UDPTimeoutTest ]
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1948
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1949 tests.extend([
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1950 NonBlockingTCPTests,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1951 FileObjectClassTestCase,
58146
efbac7d3a368 Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents: 58136
diff changeset
1952 FileObjectInterruptedTestCase,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1953 UnbufferedFileObjectClassTestCase,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1954 LineBufferedFileObjectClassTestCase,
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1955 SmallBufferedFileObjectClassTestCase,
65258
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1956 UnicodeReadFileObjectClassTestCase,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1957 UnicodeWriteFileObjectClassTestCase,
1875bfed5689 Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64959
diff changeset
1958 UnicodeReadWriteFileObjectClassTestCase,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1959 NetworkConnectionNoServer,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1960 NetworkConnectionAttributesTest,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
1961 NetworkConnectionBehaviourTest,
64574
caf0b4b0569e Fix issue 9794: adds context manager protocol to socket.socket so that socket.create_connection() can be used with the 'with' statement.
Giampaolo Rodolà <g.rodola@gmail.com>
parents: 64534
diff changeset
1962 ContextManagersTest,
65314
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1963 CloexecConstantTest,
12442ac3f7dd Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents: 65258
diff changeset
1964 NonblockConstantTest
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28833
diff changeset
1965 ])
33063
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1966 if hasattr(socket, "socketpair"):
90da70986407 Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents: 32630
diff changeset
1967 tests.append(BasicSocketPairTest)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1968 if sys.platform == 'linux2':
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 35453
diff changeset
1969 tests.append(TestLinuxAbstractNamespace)
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1970 if isTipcAvailable():
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
1971 tests.append(TIPCTest)
44587
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44568
diff changeset
1972 tests.append(TIPCThreadableTest)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
1973
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
1974 thread_info = support.threading_setup()
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
1975 support.run_unittest(*tests)
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
1976 support.threading_cleanup(*thread_info)
23790
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1977
486f4045d13d New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents: 23715
diff changeset
1978 if __name__ == "__main__":
24451
22570e76cca1 Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents: 24450
diff changeset
1979 test_main()