annotate Lib/test/mock_socket.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 716e601570da
children 26839edf3cc1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63651
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
1 """Mock socket module used by the smtpd and smtplib tests.
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
2 """
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
3
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
4 # imported for _GLOBAL_DEFAULT_TIMEOUT
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
5 import socket as socket_module
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
6
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
7 # Mock socket module
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
8 _defaulttimeout = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
9 _reply_data = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
10
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
11 # This is used to queue up data to be read through socket.makefile, typically
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
12 # *before* the socket object is even created. It is intended to handle a single
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
13 # line which the socket will feed on recv() or makefile().
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
14 def reply_with(line):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
15 global _reply_data
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
16 _reply_data = line
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
17
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
18
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
19 class MockFile:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
20 """Mock file object returned by MockSocket.makefile().
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
21 """
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
22 def __init__(self, lines):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
23 self.lines = lines
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
24 def readline(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
25 return self.lines.pop(0) + b'\r\n'
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
26 def close(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
27 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
28
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
29
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
30 class MockSocket:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
31 """Mock socket object used by smtpd and smtplib tests.
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
32 """
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
33 def __init__(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
34 global _reply_data
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
35 self.output = []
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
36 self.lines = []
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
37 if _reply_data:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
38 self.lines.append(_reply_data)
63728
716e601570da fix test_smtplib/test_smtpd collision through pre-loaded reply data in mock_socket
Richard Jones <richard@commonground.com.au>
parents: 63651
diff changeset
39 _reply_data = None
63651
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
40 self.conn = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
41 self.timeout = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
42
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
43 def queue_recv(self, line):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
44 self.lines.append(line)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
45
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
46 def recv(self, bufsize, flags=None):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
47 data = self.lines.pop(0) + b'\r\n'
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
48 return data
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
49
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
50 def fileno(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
51 return 0
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
52
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
53 def settimeout(self, timeout):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
54 if timeout is None:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
55 self.timeout = _defaulttimeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
56 else:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
57 self.timeout = timeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
58
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
59 def gettimeout(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
60 return self.timeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
61
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
62 def setsockopt(self, level, optname, value):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
63 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
64
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
65 def getsockopt(self, level, optname, buflen=None):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
66 return 0
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
67
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
68 def bind(self, address):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
69 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
70
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
71 def accept(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
72 self.conn = MockSocket()
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
73 return self.conn, 'c'
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
74
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
75 def getsockname(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
76 return ('0.0.0.0', 0)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
77
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
78 def setblocking(self, flag):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
79 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
80
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
81 def listen(self, backlog):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
82 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
83
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
84 def makefile(self, mode='r', bufsize=-1):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
85 handle = MockFile(self.lines)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
86 return handle
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
87
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
88 def sendall(self, buffer, flags=None):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
89 self.last = data
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
90 self.output.append(data)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
91 return len(data)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
92
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
93 def send(self, data, flags=None):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
94 self.last = data
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
95 self.output.append(data)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
96 return len(data)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
97
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
98 def getpeername(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
99 return 'peer'
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
100
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
101 def close(self):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
102 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
103
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
104
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
105 def socket(family=None, type=None, proto=None):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
106 return MockSocket()
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
107
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
108
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
109 def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
110 try:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
111 int_port = int(address[1])
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
112 except ValueError:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
113 raise error
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
114 ms = MockSocket()
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
115 if timeout is socket_module._GLOBAL_DEFAULT_TIMEOUT:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
116 timeout = getdefaulttimeout()
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
117 ms.settimeout(timeout)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
118 return ms
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
119
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
120
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
121 def setdefaulttimeout(timeout):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
122 global _defaulttimeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
123 _defaulttimeout = timeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
124
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
125
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
126 def getdefaulttimeout():
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
127 return _defaulttimeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
128
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
129
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
130 def getfqdn():
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
131 return ""
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
132
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
133
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
134 def gethostname():
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
135 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
136
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
137
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
138 def gethostbyname(name):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
139 return ""
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
140
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
141
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
142 class gaierror(Exception):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
143 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
144
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
145
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
146 class error(Exception):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
147 pass
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
148
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
149
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
150 # Constants
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
151 AF_INET = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
152 SOCK_STREAM = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
153 SOL_SOCKET = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
154 SO_REUSEADDR = None