Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_socket.py @ 66807:d28126ebd60d
Entries for datetime, callable, and collections.Counter.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Thu, 09 Dec 2010 16:41:54 +0000 |
| parents | 50d170bb71c7 |
| children | 6cb7ed7b6697 |
| 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. |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
449 self.assertEqual(b'\x00'*4, socket.inet_aton('0.0.0.0')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
450 self.assertEqual(b'\xff'*4, socket.inet_aton('255.255.255.255')) |
|
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
|
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 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
458 self.assertEqual(b'\x00\x00\x00\x00', f('0.0.0.0')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
459 self.assertEqual(b'\xff\x00\xff\x00', f('255.0.255.0')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
460 self.assertEqual(b'\xaa\xaa\xaa\xaa', f('170.170.170.170')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
461 self.assertEqual(b'\x01\x02\x03\x04', f('1.2.3.4')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
462 self.assertEqual(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 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
464 self.assertEqual(b'\x00\x00\x00\x00', g('0.0.0.0')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
465 self.assertEqual(b'\xff\x00\xff\x00', g('255.0.255.0')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
466 self.assertEqual(b'\xaa\xaa\xaa\xaa', g('170.170.170.170')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
467 self.assertEqual(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 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
480 self.assertEqual(b'\x00' * 16, f('::')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
481 self.assertEqual(b'\x00' * 16, f('0::0')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
482 self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
483 self.assertEqual( |
|
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 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
494 self.assertEqual('1.0.1.0', f(b'\x01\x00\x01\x00')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
495 self.assertEqual('170.85.170.85', f(b'\xaa\x55\xaa\x55')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
496 self.assertEqual('255.255.255.255', f(b'\xff\xff\xff\xff')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
497 self.assertEqual('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 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
499 self.assertEqual('1.0.1.0', g(b'\x01\x00\x01\x00')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
500 self.assertEqual('170.85.170.85', g(b'\xaa\x55\xaa\x55')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
501 self.assertEqual('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 |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
514 self.assertEqual('::', f(b'\x00' * 16)) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
515 self.assertEqual('::1', f(b'\x00' * 15 + b'\x01')) |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
516 self.assertEqual( |
|
28818
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. |
|
66279
c20ade63f3db
socket.gethostbyname(socket.gethostname()) can fail when host name resolution is not set up correctly; do not fail test_socket if this is the case.
Georg Brandl <georg@python.org>
parents:
65975
diff
changeset
|
547 try: |
|
c20ade63f3db
socket.gethostbyname(socket.gethostname()) can fail when host name resolution is not set up correctly; do not fail test_socket if this is the case.
Georg Brandl <georg@python.org>
parents:
65975
diff
changeset
|
548 my_ip_addr = socket.gethostbyname(socket.gethostname()) |
|
c20ade63f3db
socket.gethostbyname(socket.gethostname()) can fail when host name resolution is not set up correctly; do not fail test_socket if this is the case.
Georg Brandl <georg@python.org>
parents:
65975
diff
changeset
|
549 except socket.error: |
|
c20ade63f3db
socket.gethostbyname(socket.gethostname()) can fail when host name resolution is not set up correctly; do not fail test_socket if this is the case.
Georg Brandl <georg@python.org>
parents:
65975
diff
changeset
|
550 # Probably name lookup wasn't set up right; skip this test |
|
c20ade63f3db
socket.gethostbyname(socket.gethostname()) can fail when host name resolution is not set up correctly; do not fail test_socket if this is the case.
Georg Brandl <georg@python.org>
parents:
65975
diff
changeset
|
551 return |
|
58415
f6a626f34840
use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents:
58146
diff
changeset
|
552 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
|
553 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
|
554 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
555 def testGetSockOpt(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
556 # Testing getsockopt() |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
557 # 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
|
558 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
|
559 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
|
560 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
|
561 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
|
562 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
563 def testSetSockOpt(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
564 # Testing setsockopt() |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
565 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
|
566 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
|
567 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
|
568 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
|
569 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
|
570 |
|
24318
12815d05080c
Add test for previous core dump when sending on closed socket with
Guido van Rossum <guido@python.org>
parents:
24293
diff
changeset
|
571 def testSendAfterClose(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
572 # 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
|
573 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
|
574 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
|
575 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
|
576 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
|
577 |
|
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
|
578 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
|
579 # 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
|
580 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
|
581 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
|
582 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
|
583 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
|
584 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
|
585 |
|
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
|
586 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
|
587 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
|
588 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
|
589 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
|
590 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
|
591 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
|
592 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
|
593 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
|
594 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
|
595 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
|
596 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
|
597 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
|
598 |
|
65975
e213e465a2dc
Add socket cleanup for ResourceWarning and update test to use skip decorator
Brian Curtin <brian.curtin@gmail.com>
parents:
65925
diff
changeset
|
599 @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
|
600 def test_sock_ioctl(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
601 self.assertTrue(hasattr(socket.socket, 'ioctl')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
602 self.assertTrue(hasattr(socket, 'SIO_RCVALL')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
603 self.assertTrue(hasattr(socket, 'RCVALL_ON')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
604 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
|
605 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
|
606 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
|
607 self.addCleanup(s.close) |
|
56252
debb245ebb40
http://bugs.python.org/issue6971
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
55593
diff
changeset
|
608 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
|
609 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
|
610 |
|
63997
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
611 def testGetaddrinfo(self): |
|
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
612 try: |
|
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
613 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
|
614 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
|
615 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
|
616 # 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
|
617 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
|
618 raise |
|
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
619 # 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
|
620 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
|
621 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
|
622 # 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
|
623 # 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
|
624 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
|
625 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
|
626 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
|
627 if SUPPORTS_IPV6: |
|
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
628 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
|
629 # 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
|
630 # 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
|
631 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
|
632 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
|
633 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
|
634 # 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
|
635 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
|
636 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
|
637 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
|
638 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
|
639 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
|
640 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
|
641 # 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
|
642 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
|
643 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
|
644 # 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
|
645 # usually do this |
|
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
646 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
|
647 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
|
648 # 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
|
649 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
|
650 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
|
651 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
|
652 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
|
653 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
|
654 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
|
655 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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 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
|
663 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
|
664 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
|
665 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
|
666 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
|
667 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
|
668 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
|
669 self.assertEqual(a, b) |
|
66799
50d170bb71c7
Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66310
diff
changeset
|
670 # Issue #6697. |
|
50d170bb71c7
Issue #6697: Fixed instances of _PyUnicode_AsString() result not checked for NULL
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
66310
diff
changeset
|
671 self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') |
|
63997
b7ab77fd27b7
fix issue #8857: provide a test case for socket.getaddrinfo
Giampaolo Rodolà <g.rodola@gmail.com>
parents:
63890
diff
changeset
|
672 |
|
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
|
673 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
|
674 # 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
|
675 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
|
676 |
|
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
|
677 @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
|
678 '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
|
679 def test_idna(self): |
|
64442
5997ae2eab37
Issue #9777: test_idna requires the "network" resource
Antoine Pitrou <solipsis@pitrou.net>
parents:
64391
diff
changeset
|
680 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
|
681 # 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
|
682 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
|
683 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
|
684 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
|
685 # 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
|
686 # 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
|
687 # socket.gethostbyaddr('испытание.python.org') |
|
44535
420fe6d7d5f4
Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43954
diff
changeset
|
688 |
|
64959
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
689 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
|
690 # 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
|
691 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
|
692 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
|
693 # 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
|
694 # 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
|
695 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
|
696 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
|
697 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
|
698 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
|
699 1 // 0 |
|
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
700 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
|
701 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
|
702 try: |
|
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 # 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
|
705 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
|
706 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
|
707 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
|
708 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
|
709 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
|
710 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
|
711 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
|
712 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
|
713 finally: |
|
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
714 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
|
715 c.close() |
|
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
716 s.close() |
|
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
717 |
|
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
718 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
|
719 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
|
720 |
|
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
721 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
|
722 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
|
723 |
|
65707
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
724 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
|
725 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
|
726 r = repr(sock) |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
727 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
|
728 sock = None |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
729 support.gc_collect() |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
730 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
|
731 # 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
|
732 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
|
733 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
|
734 r = repr(sock) |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
735 sock = None |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
736 support.gc_collect() |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
737 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
|
738 f = None |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
739 support.gc_collect() |
|
cff78ffb932a
Issue #10093: ResourceWarnings are now issued when files and sockets are
Antoine Pitrou <solipsis@pitrou.net>
parents:
65606
diff
changeset
|
740 |
|
64959
aed04b3f0fe3
Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
Antoine Pitrou <solipsis@pitrou.net>
parents:
64817
diff
changeset
|
741 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
742 @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
|
743 class BasicTCPTest(SocketConnectedTest): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
744 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
745 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
|
746 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
|
747 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
748 def testRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
749 # 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
|
750 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
|
751 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
|
752 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
753 def _testRecv(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
754 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
|
755 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
756 def testOverFlowRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
757 # 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
|
758 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
|
759 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
|
760 msg = seg1 + seg2 |
|
23794
1f1a6f7dd600
Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
761 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
|
762 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
763 def _testOverFlowRecv(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
764 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
|
765 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
766 def testRecvFrom(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
767 # 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
|
768 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
|
769 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
|
770 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
771 def _testRecvFrom(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
772 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
|
773 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
774 def testOverFlowRecvFrom(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
775 # 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
|
776 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
|
777 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
|
778 msg = seg1 + seg2 |
|
23794
1f1a6f7dd600
Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
779 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
|
780 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
781 def _testOverFlowRecvFrom(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
782 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
|
783 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
784 def testSendAll(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
785 # 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
|
786 msg = b'' |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
787 while 1: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
788 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
|
789 if not read: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
790 break |
|
24675
fec0b0aef14b
testSendAll(): loop until all data is read; this was necessary at
Guido van Rossum <guido@python.org>
parents:
24674
diff
changeset
|
791 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
|
792 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
|
793 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
794 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
|
795 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
|
796 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
|
797 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
798 def testFromFd(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
799 # Testing fromfd() |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
800 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
|
801 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
|
802 self.addCleanup(sock.close) |
|
64747
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64593
diff
changeset
|
803 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
|
804 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
|
805 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
|
806 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
807 def _testFromFd(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
808 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
|
809 |
|
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
|
810 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
|
811 # 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
|
812 sock = self.cli_conn.dup() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
813 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
|
814 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
|
815 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
|
816 |
|
70cfc7b504c2
This is roughly socket2.diff from issue 1378, with a few changes applied
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
817 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
|
818 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
|
819 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
820 def testShutdown(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
821 # Testing shutdown() |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
822 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
|
823 self.assertEqual(msg, MSG) |
|
50800
b17ec4c4a7c3
Merged revisions 68611-68613 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
50741
diff
changeset
|
824 # 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
|
825 # 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
|
826 # 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
|
827 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
|
828 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
829 def _testShutdown(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
830 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
|
831 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
|
832 |
|
63890
60076f28c023
Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents:
63856
diff
changeset
|
833 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
|
834 # Testing detach() |
|
60076f28c023
Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents:
63856
diff
changeset
|
835 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
|
836 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
|
837 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
|
838 # 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
|
839 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
|
840 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
|
841 # ...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
|
842 # 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
|
843 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
|
844 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
|
845 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
|
846 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
|
847 |
|
63890
60076f28c023
Followup to r83869 and issue #8524: rename socket.forget() to socket.detach()
Antoine Pitrou <solipsis@pitrou.net>
parents:
63856
diff
changeset
|
848 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
|
849 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
|
850 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
851 @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
|
852 class BasicUDPTest(ThreadedUDPSocketTest): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
853 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
854 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
|
855 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
|
856 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
857 def testSendtoAndRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
858 # 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
|
859 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
|
860 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
|
861 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
862 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
|
863 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
|
864 |
|
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
|
865 def testRecvFrom(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
866 # 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
|
867 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
|
868 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
|
869 |
|
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
|
870 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
|
871 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
|
872 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
873 def testRecvFromNegative(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
874 # 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
|
875 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
|
876 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
877 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
|
878 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
|
879 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
880 @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
|
881 class TCPCloserTest(ThreadedTCPSocketTest): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
882 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
883 def testClose(self): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
884 conn, addr = self.serv.accept() |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
885 conn.close() |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
886 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
887 sd = self.cli |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
888 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
|
889 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
|
890 self.assertEqual(sd.recv(1), b'') |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
891 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
892 # 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
|
893 conn.close() |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
894 conn.close() |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
895 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
896 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
|
897 self.cli.connect((HOST, self.port)) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
898 time.sleep(1.0) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
899 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
900 @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
|
901 class BasicSocketPairTest(SocketPairTest): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
902 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
903 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
|
904 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
|
905 |
|
64754
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
906 def _check_defaults(self, sock): |
|
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
907 self.assertIsInstance(sock, socket.socket) |
|
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
908 if hasattr(socket, 'AF_UNIX'): |
|
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
909 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
|
910 else: |
|
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
911 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
|
912 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
|
913 self.assertEqual(sock.proto, 0) |
|
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
914 |
|
64747
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64593
diff
changeset
|
915 def _testDefaults(self): |
|
64754
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
916 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
|
917 |
|
534853ba8cd3
Issue #1552: socket.socketpair() now returns regular socket.socket
Antoine Pitrou <solipsis@pitrou.net>
parents:
64593
diff
changeset
|
918 def testDefaults(self): |
|
64754
b81e8e3fa5bb
Make testDefaults in test.test_socket.BasicSocketPairTest more reliable.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64747
diff
changeset
|
919 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
|
920 |
|
33063
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
921 def testRecv(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
922 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
|
923 self.assertEqual(msg, 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 _testRecv(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
926 self.cli.send(MSG) |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
927 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
928 def testSend(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
929 self.serv.send(MSG) |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
930 |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
931 def _testSend(self): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
932 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
|
933 self.assertEqual(msg, MSG) |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
934 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
935 @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
|
936 class NonBlockingTCPTests(ThreadedTCPSocketTest): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
937 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
938 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
|
939 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
|
940 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
941 def testSetBlocking(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
942 # 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
|
943 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
|
944 start = time.time() |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
945 try: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
946 self.serv.accept() |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
947 except socket.error: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
948 pass |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
949 end = time.time() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
950 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
|
951 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
952 def _testSetBlocking(self): |
|
17208
c8817a9bbbdd
Make socket.getservbyname test optional on socket module having that
Barry Warsaw <barry@python.org>
parents:
16519
diff
changeset
|
953 pass |
|
4880
9a7e9f10aed6
Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
954 |
|
65314
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
955 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
|
956 def testInitNonBlocking(self): |
|
65606
a30e75820b8d
Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents:
65604
diff
changeset
|
957 v = linux_version() |
|
a30e75820b8d
Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents:
65604
diff
changeset
|
958 if v < (2, 6, 28): |
|
a30e75820b8d
Also guard other SOCK_NONBLOCK test
Antoine Pitrou <solipsis@pitrou.net>
parents:
65604
diff
changeset
|
959 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
|
960 % ".".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
|
961 # 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
|
962 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
|
963 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
|
964 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
|
965 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
|
966 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
|
967 # actual testing |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
968 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
|
969 try: |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
970 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
|
971 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
|
972 pass |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
973 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
|
974 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
|
975 |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
976 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
|
977 pass |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
978 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
979 def testAccept(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
980 # 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
|
981 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
|
982 try: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
983 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
|
984 except socket.error: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
985 pass |
|
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 non-blocking accept.") |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
988 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
|
989 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
|
990 conn, addr = self.serv.accept() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
991 conn.close() |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
992 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
993 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
|
994 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
995 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
|
996 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
|
997 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
|
998 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
999 def testConnect(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1000 # 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
|
1001 conn, addr = self.serv.accept() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1002 conn.close() |
|
4880
9a7e9f10aed6
Test of the socket module. The following functions and methods are
Barry Warsaw <barry@python.org>
parents:
diff
changeset
|
1003 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1004 def _testConnect(self): |
|
23818
8a0a14e3725e
Fix non-blocking connect() for Windows. Refactored the code
Guido van Rossum <guido@python.org>
parents:
23815
diff
changeset
|
1005 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
|
1006 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
|
1007 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1008 def testRecv(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1009 # 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
|
1010 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
|
1011 conn.setblocking(0) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1012 try: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1013 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
|
1014 except socket.error: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1015 pass |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1016 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1017 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
|
1018 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
|
1019 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
|
1020 msg = conn.recv(len(MSG)) |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1021 conn.close() |
|
23794
1f1a6f7dd600
Lose the message on assertEqual calls -- they actually hide
Guido van Rossum <guido@python.org>
parents:
23790
diff
changeset
|
1022 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
|
1023 else: |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1024 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
|
1025 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1026 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
|
1027 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
|
1028 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
|
1029 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
|
1030 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
1031 @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
|
1032 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
|
1033 """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
|
1034 |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1035 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
|
1036 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
|
1037 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
|
1038 |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1039 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
|
1040 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
|
1041 to the client. |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
1042 """ |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21315
diff
changeset
|
1043 |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1044 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
|
1045 encoding = 'utf8' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1046 errors = 'strict' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1047 newline = None |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1048 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1049 read_mode = 'rb' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1050 read_msg = MSG |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1051 write_mode = 'wb' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1052 write_msg = MSG |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1053 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1054 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
|
1055 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
|
1056 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1057 def setUp(self): |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1058 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
|
1059 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
|
1060 SocketConnectedTest.setUp(self) |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1061 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
|
1062 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
|
1063 encoding = self.encoding, |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1064 errors = self.errors, |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1065 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
|
1066 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1067 def tearDown(self): |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1068 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
|
1069 self.read_file.close() |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1070 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
|
1071 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
|
1072 SocketConnectedTest.tearDown(self) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1073 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1074 def clientSetUp(self): |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1075 SocketConnectedTest.clientSetUp(self) |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1076 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
|
1077 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
|
1078 encoding = self.encoding, |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1079 errors = self.errors, |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1080 newline = self.newline) |
|
23715
c2733e8f6612
SF patch 555085 (timeout socket implementation) by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
21315
diff
changeset
|
1081 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1082 def clientTearDown(self): |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1083 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
|
1084 self.write_file.close() |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1085 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
|
1086 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
|
1087 SocketConnectedTest.clientTearDown(self) |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1088 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1089 def testSmallRead(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1090 # 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
|
1091 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
|
1092 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
|
1093 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
|
1094 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
|
1095 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1096 def _testSmallRead(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1097 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
|
1098 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
|
1099 |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1100 def testFullRead(self): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1101 # read until EOF |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1102 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
|
1103 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
|
1104 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1105 def _testFullRead(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1106 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
|
1107 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
|
1108 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1109 def testUnbufferedRead(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1110 # 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
|
1111 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
|
1112 while 1: |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1113 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
|
1114 if not char: |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1115 break |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1116 buf += char |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1117 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
|
1118 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1119 def _testUnbufferedRead(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1120 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
|
1121 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
|
1122 |
|
23790
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1123 def testReadline(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1124 # 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
|
1125 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
|
1126 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
|
1127 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1128 def _testReadline(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1129 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
|
1130 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
|
1131 |
|
42434
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
1132 def testCloseAfterMakefile(self): |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
1133 # 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
|
1134 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
|
1135 # read until EOF |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1136 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
|
1137 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
|
1138 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
1139 def _testCloseAfterMakefile(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1140 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
|
1141 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
|
1142 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
1143 def testMakefileAfterMakefileClose(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1144 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
|
1145 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
|
1146 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
|
1147 msg = msg.decode() |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1148 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
|
1149 |
|
c35140559f87
Make sure socket.close() doesn't interfere with socket.makefile().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
42169
diff
changeset
|
1150 def _testMakefileAfterMakefileClose(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1151 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
|
1152 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
|
1153 |
|
31833
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
1154 def testClosedAttr(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1155 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
|
1156 |
|
a910b822b12c
SF bug 924242: socket._fileobject._getclosed() returns wrong value
Tim Peters <tim.peters@gmail.com>
parents:
31503
diff
changeset
|
1157 def _testClosedAttr(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1158 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
|
1159 |
|
49782
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48623
diff
changeset
|
1160 def testAttributes(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1161 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
|
1162 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
|
1163 |
|
5ffd67df8c9f
Follow-up of r67300: correct a failure in socket.makefile().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48623
diff
changeset
|
1164 def _testAttributes(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1165 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
|
1166 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
|
1167 |
|
50741
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1168 def testRealClose(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1169 self.read_file.close() |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1170 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
|
1171 self.cli_conn.close() |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1172 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
|
1173 |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1174 def _testRealClose(self): |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1175 pass |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1176 |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1177 |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1178 class FileObjectInterruptedTestCase(unittest.TestCase): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1179 """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
|
1180 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1181 class MockSocket(object): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1182 def __init__(self, recv_funcs=()): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1183 # 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
|
1184 # call to recv(). |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1185 self._recv_step = iter(recv_funcs) |
|
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 recv_into(self, buffer): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1188 data = next(self._recv_step)() |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1189 assert len(buffer) >= len(data) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1190 buffer[:len(data)] = data |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1191 return len(data) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1192 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1193 def _decref_socketios(self): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1194 pass |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1195 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1196 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
|
1197 raw = socket.SocketIO(self, "r") |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1198 if buffering < 0: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1199 buffering = io.DEFAULT_BUFFER_SIZE |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1200 if buffering == 0: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1201 return raw |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1202 buffer = io.BufferedReader(raw, buffering) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1203 text = io.TextIOWrapper(buffer, None, None) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1204 text.mode = "rb" |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1205 return text |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1206 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1207 @staticmethod |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1208 def _raise_eintr(): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1209 raise socket.error(errno.EINTR) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1210 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1211 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
|
1212 raw = socket.SocketIO(mock, "r") |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1213 if buffering < 0: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1214 buffering = io.DEFAULT_BUFFER_SIZE |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1215 if buffering == 0: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1216 return raw |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1217 buffer = io.BufferedReader(raw, buffering) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1218 text = io.TextIOWrapper(buffer, None, None) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1219 text.mode = "rb" |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1220 return text |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1221 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1222 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
|
1223 mock_sock = self.MockSocket(recv_funcs=[ |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1224 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
|
1225 self._raise_eintr, |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1226 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
|
1227 lambda : b"", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1228 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
|
1229 ]) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1230 fo = mock_sock._textiowrap_for_test(buffering=buffering) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
1231 self.assertEqual(fo.readline(size), "This is the first line\n") |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
1232 self.assertEqual(fo.readline(size), "And the second line is here\n") |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1233 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1234 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
|
1235 mock_sock = self.MockSocket(recv_funcs=[ |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1236 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
|
1237 self._raise_eintr, |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1238 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
|
1239 lambda : b"", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1240 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
|
1241 ]) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1242 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
|
1243 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
|
1244 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
|
1245 if buffering == 0: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1246 data = b'' |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1247 else: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1248 data = '' |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1249 expecting = expecting.decode('utf8') |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1250 while len(data) != len(expecting): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1251 part = fo.read(size) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1252 if not part: |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1253 break |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1254 data += part |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
1255 self.assertEqual(data, expecting) |
|
58146
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_default(self): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1258 self._test_readline() |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1259 self._test_readline(size=100) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1260 self._test_read() |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1261 self._test_read(size=100) |
|
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_with_1k_buffer(self): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1264 self._test_readline(buffering=1024) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1265 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
|
1266 self._test_read(buffering=1024) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1267 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
|
1268 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1269 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
|
1270 mock_sock = self.MockSocket(recv_funcs=[ |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1271 lambda : b"a", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1272 lambda : b"\n", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1273 lambda : b"B", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1274 self._raise_eintr, |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1275 lambda : b"b", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1276 lambda : b"", |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1277 ]) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1278 fo = mock_sock._textiowrap_for_test(buffering=0) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
1279 self.assertEqual(fo.readline(size), b"a\n") |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
1280 self.assertEqual(fo.readline(size), b"Bb") |
|
58146
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 def test_no_buffer(self): |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1283 self._test_readline_no_buffer() |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1284 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
|
1285 self._test_read(buffering=0) |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1286 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
|
1287 |
|
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1288 |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1289 class UnbufferedFileObjectClassTestCase(FileObjectClassTestCase): |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1290 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1291 """Repeat the tests from FileObjectClassTestCase with bufsize==0. |
|
24674
3269bdd65988
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
24655
diff
changeset
|
1292 |
|
24634
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1293 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
|
1294 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
|
1295 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
|
1296 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
|
1297 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
|
1298 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1299 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
|
1300 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1301 def testUnbufferedReadline(self): |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1302 # 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
|
1303 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
|
1304 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
|
1305 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
|
1306 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
|
1307 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
|
1308 |
|
778093533a46
"Unbuffered" mode of class _fileobject wasn't actually unbuffered,
Guido van Rossum <guido@python.org>
parents:
24485
diff
changeset
|
1309 def _testUnbufferedReadline(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1310 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
|
1311 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
|
1312 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
|
1313 |
|
50741
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1314 def testMakefileClose(self): |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1315 # 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
|
1316 self.cli_conn.close() |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1317 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
|
1318 self.assertEqual(msg, self.read_msg) |
|
50741
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1319 # ...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
|
1320 self.read_file.close() |
|
50741
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1321 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
|
1322 |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1323 def _testMakefileClose(self): |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1324 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
|
1325 self.write_file.flush() |
|
50741
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 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
|
1329 self.read_file.close() |
|
50741
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1330 refcount_after = sys.getrefcount(self.cli_conn) |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1331 self.assertEqual(refcount_before - 1, refcount_after) |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1332 |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1333 def _testMakefileCloseSocketDestroy(self): |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1334 pass |
|
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1335 |
|
64817
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1336 # Non-blocking ops |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1337 # 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
|
1338 # `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
|
1339 |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1340 def testSmallReadNonBlocking(self): |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1341 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
|
1342 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
|
1343 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
|
1344 self.evt1.set() |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1345 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
|
1346 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
|
1347 buf = bytearray(10) |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1348 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
|
1349 self.assertEqual(n, 3) |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1350 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
|
1351 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
|
1352 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
|
1353 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
|
1354 |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1355 def _testSmallReadNonBlocking(self): |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1356 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
|
1357 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
|
1358 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
|
1359 self.evt2.set() |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1360 # 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
|
1361 # 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
|
1362 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
|
1363 |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1364 def testWriteNonBlocking(self): |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1365 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
|
1366 # 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
|
1367 # 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
|
1368 if self.serv_skipped: |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1369 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
|
1370 |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1371 def _testWriteNonBlocking(self): |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1372 self.serv_skipped = None |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1373 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
|
1374 # 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
|
1375 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
|
1376 LIMIT = 10 |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1377 # 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
|
1378 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
|
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 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
|
1381 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
|
1382 if n is None: |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1383 # Succeeded |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1384 break |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1385 self.assertGreater(n, 0) |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1386 else: |
|
f3dcb314c3e3
Issue #9854: SocketIO objects now observe the RawIOBase interface in
Antoine Pitrou <solipsis@pitrou.net>
parents:
64754
diff
changeset
|
1387 # 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
|
1388 # 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
|
1389 # 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
|
1390 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
|
1391 |
|
50741
5f4c2de95091
Fixes issue #3826 and #4791:
Gregory P. Smith <greg@mad-scientist.com>
parents:
49800
diff
changeset
|
1392 |
|
24655
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1393 class LineBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1394 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1395 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
|
1396 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1397 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1398 class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1399 |
|
18ca4e783f98
Replace docstrings on test functions witrh comments -- then unittest
Guido van Rossum <guido@python.org>
parents:
24648
diff
changeset
|
1400 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
|
1401 |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
1402 |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1403 class UnicodeReadFileObjectClassTestCase(FileObjectClassTestCase): |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1404 """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
|
1405 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1406 read_mode = 'r' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1407 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
|
1408 write_mode = 'wb' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1409 write_msg = MSG |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1410 newline = '' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1411 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1412 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1413 class UnicodeWriteFileObjectClassTestCase(FileObjectClassTestCase): |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1414 """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
|
1415 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1416 read_mode = 'rb' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1417 read_msg = MSG |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1418 write_mode = 'w' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1419 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
|
1420 newline = '' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1421 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1422 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1423 class UnicodeReadWriteFileObjectClassTestCase(FileObjectClassTestCase): |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1424 """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
|
1425 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1426 read_mode = 'r' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1427 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
|
1428 write_mode = 'w' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1429 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
|
1430 newline = '' |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1431 |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1432 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1433 class NetworkConnectionTest(object): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1434 """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
|
1435 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1436 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
|
1437 # 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
|
1438 # 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
|
1439 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
|
1440 self.serv_conn = self.cli |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1441 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1442 class BasicTCPTest2(NetworkConnectionTest, BasicTCPTest): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1443 """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
|
1444 """ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1445 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1446 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
|
1447 |
|
64534
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1448 class MockSocket(socket.socket): |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1449 def connect(self, *args): |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1450 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
|
1451 |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1452 @contextlib.contextmanager |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1453 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
|
1454 """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
|
1455 old_socket = socket.socket |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1456 socket.socket = self.MockSocket |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1457 try: |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1458 yield |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1459 finally: |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1460 socket.socket = old_socket |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1461 |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1462 def test_connect(self): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46899
diff
changeset
|
1463 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
|
1464 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
|
1465 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
|
1466 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
|
1467 cli.connect((HOST, port)) |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1468 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
|
1469 |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1470 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
|
1471 # 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
|
1472 # a proper errno attribute. |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1473 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
|
1474 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
|
1475 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
|
1476 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
|
1477 |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1478 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
|
1479 # 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
|
1480 # as generic socket errors. |
|
b2f7cf9c6776
Issue #9792: In case of connection failure, socket.create_connection()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64442
diff
changeset
|
1481 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
|
1482 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
|
1483 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
|
1484 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1485 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
1486 @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
|
1487 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1488 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1489 def __init__(self, methodName='runTest'): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1490 SocketTCPTest.__init__(self, methodName=methodName) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1491 ThreadableTest.__init__(self) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1492 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1493 def clientSetUp(self): |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
56367
diff
changeset
|
1494 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
|
1495 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1496 def clientTearDown(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1497 self.cli.close() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1498 self.cli = None |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1499 ThreadableTest.clientTearDown(self) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1500 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1501 def _justAccept(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1502 conn, addr = self.serv.accept() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1503 conn.close() |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1504 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1505 testFamily = _justAccept |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1506 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
|
1507 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
|
1508 self.addCleanup(self.cli.close) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1509 self.assertEqual(self.cli.family, 2) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1510 |
|
58136
ecc8a45894c9
Merged revisions 77286 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
1511 testSourceAddress = _justAccept |
|
ecc8a45894c9
Merged revisions 77286 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58114
diff
changeset
|
1512 def _testSourceAddress(self): |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
56367
diff
changeset
|
1513 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
|
1514 source_address=('', self.source_port)) |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1515 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
|
1516 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
|
1517 # 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
|
1518 # call happened. |
|
58114
b0386fe650c7
Merged revisions 77263-77264 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
56367
diff
changeset
|
1519 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1520 testTimeoutDefault = _justAccept |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1521 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
|
1522 # 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
|
1523 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
|
1524 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
|
1525 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
|
1526 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
|
1527 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
|
1528 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
|
1529 socket.setdefaulttimeout(None) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66279
diff
changeset
|
1530 self.assertEqual(self.cli.gettimeout(), 42) |
|
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
|
1531 |
|
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 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
|
1533 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
|
1534 # 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
|
1535 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
|
1536 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
|
1537 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
|
1538 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
|
1539 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
|
1540 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
|
1541 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
|
1542 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
|
1543 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1544 testTimeoutValueNamed = _justAccept |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1545 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
|
1546 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
|
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 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1549 testTimeoutValueNonamed = _justAccept |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1550 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
|
1551 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
|
1552 self.addCleanup(self.cli.close) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1553 self.assertEqual(self.cli.gettimeout(), 30) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1554 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
1555 @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
|
1556 class NetworkConnectionBehaviourTest(SocketTCPTest, ThreadableTest): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1557 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1558 def __init__(self, methodName='runTest'): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1559 SocketTCPTest.__init__(self, methodName=methodName) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1560 ThreadableTest.__init__(self) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1561 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1562 def clientSetUp(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1563 pass |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1564 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1565 def clientTearDown(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1566 self.cli.close() |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1567 self.cli = None |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1568 ThreadableTest.clientTearDown(self) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1569 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1570 def testInsideTimeout(self): |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1571 conn, addr = self.serv.accept() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1572 self.addCleanup(conn.close) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1573 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
|
1574 conn.send(b"done!") |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1575 testOutsideTimeout = testInsideTimeout |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1576 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1577 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
|
1578 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
|
1579 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
|
1580 self.assertEqual(data, b"done!") |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1581 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1582 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
|
1583 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
|
1584 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
|
1585 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1586 |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1587 class TCPTimeoutTest(SocketTCPTest): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1588 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1589 def testTCPTimeout(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1590 def raise_timeout(*args, **kwargs): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1591 self.serv.settimeout(1.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1592 self.serv.accept() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
1593 self.assertRaises(socket.timeout, raise_timeout, |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1594 "Error generating a timeout exception (TCP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1595 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1596 def testTimeoutZero(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1597 ok = False |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1598 try: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1599 self.serv.settimeout(0.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1600 foo = self.serv.accept() |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1601 except socket.timeout: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1602 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
|
1603 except socket.error: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1604 ok = True |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1605 except: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1606 self.fail("caught unexpected exception (TCP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1607 if not ok: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1608 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
|
1609 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1610 def testInterruptedTimeout(self): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1611 # 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
|
1612 # 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
|
1613 # 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
|
1614 if not hasattr(signal, "alarm"): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1615 return # can only test on *nix |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1616 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
|
1617 class Alarm(Exception): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1618 pass |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1619 def alarm_handler(signal, frame): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1620 raise Alarm |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1621 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
|
1622 try: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1623 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
|
1624 try: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1625 foo = self.serv.accept() |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1626 except socket.timeout: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1627 self.fail("caught timeout instead of Alarm") |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1628 except Alarm: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1629 pass |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1630 except: |
|
46161
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
1631 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
|
1632 " %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
|
1633 (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
|
1634 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1635 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
|
1636 finally: |
|
848160d10780
Merged revisions 61981,61984-61987,61992-61993,61997-62000 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44782
diff
changeset
|
1637 signal.alarm(0) # shut off alarm |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1638 except Alarm: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1639 self.fail("got Alarm in wrong place") |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1640 finally: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1641 # 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
|
1642 signal.signal(signal.SIGALRM, old_alarm) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1643 |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1644 class UDPTimeoutTest(SocketTCPTest): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1645 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1646 def testUDPTimeout(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1647 def raise_timeout(*args, **kwargs): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1648 self.serv.settimeout(1.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1649 self.serv.recv(1024) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
1650 self.assertRaises(socket.timeout, raise_timeout, |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1651 "Error generating a timeout exception (UDP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1652 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1653 def testTimeoutZero(self): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1654 ok = False |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1655 try: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1656 self.serv.settimeout(0.0) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1657 foo = self.serv.recv(1024) |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1658 except socket.timeout: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1659 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
|
1660 except socket.error: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1661 ok = True |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1662 except: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1663 self.fail("caught unexpected exception (UDP)") |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1664 if not ok: |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1665 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
|
1666 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1667 class TestExceptions(unittest.TestCase): |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1668 |
|
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1669 def testExceptionTree(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
1670 self.assertTrue(issubclass(socket.error, Exception)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
1671 self.assertTrue(issubclass(socket.herror, socket.error)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
1672 self.assertTrue(issubclass(socket.gaierror, socket.error)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51579
diff
changeset
|
1673 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
|
1674 |
|
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 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
|
1676 |
|
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 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
|
1678 |
|
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 def testLinuxAbstractNamespace(self): |
| 42596 | 1680 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
|
1681 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
|
1682 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
|
1683 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
|
1684 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
|
1685 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
|
1686 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
|
1687 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
|
1688 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
|
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 testMaxName(self): |
| 42596 | 1691 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
|
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 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 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
|
1695 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
35453
diff
changeset
|
1696 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
|
1697 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
|
1698 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
|
1699 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
|
1700 |
|
29662
1308d7ec9953
SF patch #760257: add socket.timeout exception
Raymond Hettinger <python@rcn.com>
parents:
29087
diff
changeset
|
1701 |
|
61073
493bb4585e38
Merged revisions 80552-80556,80564-80566,80568-80571 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59677
diff
changeset
|
1702 @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
|
1703 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
|
1704 """ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1705 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
|
1706 """ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
1707 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
|
1708 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
|
1709 |
|
59677
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1710 def testRecvIntoArray(self): |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1711 buf = bytearray(1024) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1712 nbytes = self.cli_conn.recv_into(buf) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1713 self.assertEqual(nbytes, len(MSG)) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1714 msg = buf[:len(MSG)] |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1715 self.assertEqual(msg, MSG) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1716 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1717 def _testRecvIntoArray(self): |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1718 buf = bytes(MSG) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1719 self.serv_conn.send(buf) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1720 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1721 def testRecvIntoBytearray(self): |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
47615
diff
changeset
|
1722 buf = bytearray(1024) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1723 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
|
1724 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
|
1725 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
|
1726 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
|
1727 |
|
59677
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1728 _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
|
1729 |
|
59677
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1730 def testRecvIntoMemoryview(self): |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1731 buf = bytearray(1024) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1732 nbytes = self.cli_conn.recv_into(memoryview(buf)) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1733 self.assertEqual(nbytes, len(MSG)) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1734 msg = buf[:len(MSG)] |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1735 self.assertEqual(msg, MSG) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1736 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1737 _testRecvIntoMemoryview = _testRecvIntoArray |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1738 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1739 def testRecvFromIntoArray(self): |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
47615
diff
changeset
|
1740 buf = bytearray(1024) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1741 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
|
1742 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
|
1743 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
|
1744 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
|
1745 |
|
59677
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1746 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
|
1747 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
|
1748 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
|
1749 |
|
59677
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1750 def testRecvFromIntoBytearray(self): |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1751 buf = bytearray(1024) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1752 nbytes, addr = self.cli_conn.recvfrom_into(buf) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1753 self.assertEqual(nbytes, len(MSG)) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1754 msg = buf[:len(MSG)] |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1755 self.assertEqual(msg, MSG) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1756 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1757 _testRecvFromIntoBytearray = _testRecvFromIntoArray |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1758 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1759 def testRecvFromIntoMemoryview(self): |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1760 buf = bytearray(1024) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1761 nbytes, addr = self.cli_conn.recvfrom_into(memoryview(buf)) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1762 self.assertEqual(nbytes, len(MSG)) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1763 msg = buf[:len(MSG)] |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1764 self.assertEqual(msg, MSG) |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1765 |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1766 _testRecvFromIntoMemoryview = _testRecvFromIntoArray |
|
d62ca1eaccb9
NOTE: just porting tests here.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59522
diff
changeset
|
1767 |
|
44568
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1768 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1769 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
|
1770 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
|
1771 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
|
1772 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1773 def isTipcAvailable(): |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1774 """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
|
1775 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1776 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
|
1777 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
|
1778 """ |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1779 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
|
1780 return False |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1781 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
|
1782 return False |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1783 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
|
1784 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
|
1785 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
|
1786 return True |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46899
diff
changeset
|
1787 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
|
1788 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
|
1789 return False |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1790 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1791 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
|
1792 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
|
1793 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
|
1794 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
|
1795 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1796 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
|
1797 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
|
1798 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
|
1799 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
|
1800 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1801 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
|
1802 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
|
1803 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
|
1804 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1805 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
|
1806 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1807 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
|
1808 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
|
1809 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1810 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1811 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
|
1812 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
|
1813 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
|
1814 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
|
1815 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1816 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
|
1817 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
|
1818 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
|
1819 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
|
1820 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
|
1821 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
|
1822 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
|
1823 self.serverExplicitReady() |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1824 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
|
1825 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1826 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
|
1827 # 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
|
1828 # 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
|
1829 # 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
|
1830 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
|
1831 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
|
1832 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
|
1833 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
|
1834 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
|
1835 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
|
1836 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1837 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
|
1838 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
|
1839 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
|
1840 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
|
1841 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1842 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
|
1843 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
|
1844 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
|
1845 |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1846 |
|
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
|
1847 @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
|
1848 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
|
1849 |
|
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 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
|
1851 # 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
|
1852 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
|
1853 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
|
1854 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
|
1855 # 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
|
1856 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
|
1857 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
|
1858 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
|
1859 # 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
|
1860 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
|
1861 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
|
1862 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
|
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 conn, addr = self.serv.accept() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1866 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
|
1867 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
|
1868 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
|
1869 |
|
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 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
|
1871 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
|
1872 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
|
1873 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
|
1874 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
|
1875 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
|
1876 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
|
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 conn, addr = self.serv.accept() |
|
65882
889fd8ecbff4
Clean up ResourceWarnings due to unclosed sockets.
Brian Curtin <brian.curtin@gmail.com>
parents:
65707
diff
changeset
|
1880 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
|
1881 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
|
1882 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
|
1883 |
|
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 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
|
1885 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
|
1886 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
|
1887 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
|
1888 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
|
1889 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
|
1890 |
|
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 |
|
65314
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1892 @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
|
1893 "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
|
1894 @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
|
1895 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
|
1896 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
|
1897 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
|
1898 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
|
1899 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
|
1900 % ".".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
|
1901 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
|
1902 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
|
1903 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
|
1904 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
|
1905 |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1906 |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1907 @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
|
1908 "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
|
1909 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
|
1910 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
|
1911 if nonblock: |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1912 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
|
1913 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
|
1914 else: |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1915 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
|
1916 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
|
1917 |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1918 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
|
1919 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
|
1920 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
|
1921 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
|
1922 % ".".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
|
1923 # 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
|
1924 # 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
|
1925 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
|
1926 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
|
1927 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
|
1928 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
|
1929 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
|
1930 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
|
1931 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
|
1932 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
|
1933 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
|
1934 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
|
1935 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
|
1936 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
|
1937 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
|
1938 # defaulttimeout |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1939 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
|
1940 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
|
1941 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
|
1942 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
|
1943 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
|
1944 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
|
1945 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
|
1946 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
|
1947 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
|
1948 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
|
1949 |
|
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
|
1950 |
|
24451
22570e76cca1
Enable test_socket again, if only to prevent mistakes like Jeremy
Guido van Rossum <guido@python.org>
parents:
24450
diff
changeset
|
1951 def test_main(): |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1952 tests = [GeneralModuleTests, BasicTCPTest, TCPCloserTest, TCPTimeoutTest, |
|
61259
1f7506f23771
Remove traces of MacOS9 support.
Ronald Oussoren <ronaldoussoren@mac.com>
parents:
61073
diff
changeset
|
1953 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
|
1954 |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1955 tests.extend([ |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1956 NonBlockingTCPTests, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1957 FileObjectClassTestCase, |
|
58146
efbac7d3a368
Merged revisions 74426 via svnmerge from
Gregory P. Smith <greg@mad-scientist.com>
parents:
58136
diff
changeset
|
1958 FileObjectInterruptedTestCase, |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1959 UnbufferedFileObjectClassTestCase, |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1960 LineBufferedFileObjectClassTestCase, |
|
40737
6ebad2a6b990
Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40644
diff
changeset
|
1961 SmallBufferedFileObjectClassTestCase, |
|
65258
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1962 UnicodeReadFileObjectClassTestCase, |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1963 UnicodeWriteFileObjectClassTestCase, |
|
1875bfed5689
Issue #10041: The signature of optional arguments in socket.makefile()
Antoine Pitrou <solipsis@pitrou.net>
parents:
64959
diff
changeset
|
1964 UnicodeReadWriteFileObjectClassTestCase, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1965 NetworkConnectionNoServer, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1966 NetworkConnectionAttributesTest, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40737
diff
changeset
|
1967 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
|
1968 ContextManagersTest, |
|
65314
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1969 CloexecConstantTest, |
|
12442ac3f7dd
Issue #7523: Add SOCK_CLOEXEC and SOCK_NONBLOCK to the socket module,
Antoine Pitrou <solipsis@pitrou.net>
parents:
65258
diff
changeset
|
1970 NonblockConstantTest |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28833
diff
changeset
|
1971 ]) |
|
33063
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
1972 if hasattr(socket, "socketpair"): |
|
90da70986407
Patch #1003700: Add socketpair function to socket module.
Dave Cole <djc@object-craft.com.au>
parents:
32630
diff
changeset
|
1973 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
|
1974 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
|
1975 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
|
1976 if isTipcAvailable(): |
|
305d7d2ce047
Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents:
44535
diff
changeset
|
1977 tests.append(TIPCTest) |
|
44587
fb5a6330f71b
Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44568
diff
changeset
|
1978 tests.append(TIPCThreadableTest) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
1979 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46899
diff
changeset
|
1980 thread_info = support.threading_setup() |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46899
diff
changeset
|
1981 support.run_unittest(*tests) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
46899
diff
changeset
|
1982 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
|
1983 |
|
486f4045d13d
New test suite for the socket module by Michael Gilfix.
Guido van Rossum <guido@python.org>
parents:
23715
diff
changeset
|
1984 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
|
1985 test_main() |
