annotate Lib/test/mock_socket.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents ddf0fbff94d8
children
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
71617
26839edf3cc1 Fix closes Issue11281 - smtplib.STMP gets source_address parameter, which adds the ability to bind to specific source address on a machine with multiple interfaces. Patch by Paulo Scardine.
Senthil Kumaran <senthil@uthcode.com>
parents: 63728
diff changeset
109 def create_connection(address, timeout=socket_module._GLOBAL_DEFAULT_TIMEOUT,
26839edf3cc1 Fix closes Issue11281 - smtplib.STMP gets source_address parameter, which adds the ability to bind to specific source address on a machine with multiple interfaces. Patch by Paulo Scardine.
Senthil Kumaran <senthil@uthcode.com>
parents: 63728
diff changeset
110 source_address=None):
63651
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
111 try:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
112 int_port = int(address[1])
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
113 except ValueError:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
114 raise error
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
115 ms = MockSocket()
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
116 if timeout is socket_module._GLOBAL_DEFAULT_TIMEOUT:
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
117 timeout = getdefaulttimeout()
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
118 ms.settimeout(timeout)
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
119 return ms
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
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
122 def setdefaulttimeout(timeout):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
123 global _defaulttimeout
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
124 _defaulttimeout = timeout
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
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
127 def getdefaulttimeout():
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
128 return _defaulttimeout
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
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
131 def getfqdn():
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
132 return ""
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
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
135 def gethostname():
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
136 pass
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
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
139 def gethostbyname(name):
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
140 return ""
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
80912
ddf0fbff94d8 Issue #16647: save socket error details in LMTP.connect()
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 71617
diff changeset
143 gaierror = socket_module.gaierror
ddf0fbff94d8 Issue #16647: save socket error details in LMTP.connect()
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 71617
diff changeset
144 error = socket_module.error
63651
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
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
147 # Constants
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
148 AF_INET = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
149 SOCK_STREAM = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
150 SOL_SOCKET = None
6a6c13a7f030 improvements to test_smtplib per issue2423
Richard Jones <richard@commonground.com.au>
parents:
diff changeset
151 SO_REUSEADDR = None