blob: 24aff5ebd9e2a088166b1b4adeb964b4d2715061 [file] [log] [blame]
Brett Cannonf1cfb622003-05-04 21:15:27 +00001"""Supporting definitions for the Python regression tests."""
Guido van Rossum3bead091992-01-27 17:00:37 +00002
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003if __name__ != 'test.support':
4 raise ImportError('support must be imported from the test package')
Barry Warsaw408b6d32002-07-30 23:27:12 +00005
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import contextlib
7import errno
8import socket
Fred Drakecd1b1dd2001-03-21 18:26:33 +00009import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +000010import os
11import os.path
Christian Heimes23daade02008-02-25 12:39:23 +000012import shutil
Thomas Wouters902d6eb2007-01-09 23:18:33 +000013import warnings
Guido van Rossumd8faa362007-04-27 19:54:29 +000014import unittest
Fred Drakecd1b1dd2001-03-21 18:26:33 +000015
Benjamin Peterson744c2cd2008-05-26 16:26:37 +000016__all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
17 "verbose", "use_resources", "max_memuse", "record_original_stdout",
18 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
19 "is_resource_enabled", "requires", "find_unused_port", "bind_port",
Benjamin Peterson79e48032008-05-26 17:44:33 +000020 "fcmp", "is_jython", "TESTFN", "HOST", "FUZZ", "findfile", "verify",
21 "vereq", "sortdict", "check_syntax_error", "open_urlresource",
22 "WarningMessage", "catch_warning", "CleanImport", "EnvironmentVarGuard",
23 "TransientResource", "captured_output", "captured_stdout",
24 "TransientResource", "transient_internet", "run_with_locale",
25 "set_memlimit", "bigmemtest", "bigaddrspacetest", "BasicTestRunner",
26 "run_unittest", "run_doctest", "threading_setup", "threading_cleanup",
27 "reap_children"]
Benjamin Peterson744c2cd2008-05-26 16:26:37 +000028
Fred Drake1790dd42000-07-24 06:55:00 +000029class Error(Exception):
Fred Drake004d5e62000-10-23 17:22:08 +000030 """Base class for regression test exceptions."""
Fred Drake1790dd42000-07-24 06:55:00 +000031
32class TestFailed(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000033 """Test failed."""
Fred Drake1790dd42000-07-24 06:55:00 +000034
35class TestSkipped(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000036 """Test skipped.
Fred Drake1790dd42000-07-24 06:55:00 +000037
Fred Drake004d5e62000-10-23 17:22:08 +000038 This can be raised to indicate that a test was deliberatly
39 skipped, but not because a feature wasn't available. For
40 example, if some resource can't be used, such as the network
41 appears to be unavailable, this should be raised instead of
42 TestFailed.
Fred Drake004d5e62000-10-23 17:22:08 +000043 """
Fred Drake1790dd42000-07-24 06:55:00 +000044
Fred Drake9a0db072003-02-03 15:19:30 +000045class ResourceDenied(TestSkipped):
46 """Test skipped because it requested a disallowed resource.
47
48 This is raised when a test calls requires() for a resource that
49 has not be enabled. It is used to distinguish between expected
50 and unexpected skips.
51 """
52
Benjamin Peterson699adb92008-05-08 22:27:58 +000053def import_module(name, deprecated=False):
54 """Import the module to be tested, raising TestSkipped if it is not
55 available."""
56 with catch_warning(record=False):
57 if deprecated:
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +000058 warnings.filterwarnings("ignore", ".+ (module|package)",
59 DeprecationWarning)
Benjamin Peterson699adb92008-05-08 22:27:58 +000060 try:
61 module = __import__(name, level=0)
62 except ImportError:
63 raise TestSkipped("No module named " + name)
64 else:
65 return module
66
Barry Warsawc0fb6052001-08-20 22:29:23 +000067verbose = 1 # Flag set to 0 by regrtest.py
Thomas Wouters477c8d52006-05-27 19:21:47 +000068use_resources = None # Flag set to [] by regrtest.py
69max_memuse = 0 # Disable bigmem tests (they will still be run with
70 # small sizes, to make sure they work.)
Neal Norwitz3ce5d922008-08-24 07:08:55 +000071real_max_memuse = 0
Guido van Rossum531661c1996-12-20 02:58:22 +000072
Tim Peters8dee8092001-09-25 20:05:11 +000073# _original_stdout is meant to hold stdout at the time regrtest began.
74# This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
75# The point is to have some flavor of stdout the user can actually see.
76_original_stdout = None
77def record_original_stdout(stdout):
78 global _original_stdout
79 _original_stdout = stdout
80
81def get_original_stdout():
82 return _original_stdout or sys.stdout
83
Guido van Rossum3bead091992-01-27 17:00:37 +000084def unload(name):
Fred Drake004d5e62000-10-23 17:22:08 +000085 try:
86 del sys.modules[name]
87 except KeyError:
88 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000089
Neal Norwitz0e17f8c2006-01-23 07:51:27 +000090def unlink(filename):
Neal Norwitz0e17f8c2006-01-23 07:51:27 +000091 try:
92 os.unlink(filename)
93 except OSError:
94 pass
95
Christian Heimes23daade02008-02-25 12:39:23 +000096def rmtree(path):
97 try:
98 shutil.rmtree(path)
99 except OSError as e:
100 # Unix returns ENOENT, Windows returns ESRCH.
101 if e.errno not in (errno.ENOENT, errno.ESRCH):
102 raise
103
Guido van Rossum3bead091992-01-27 17:00:37 +0000104def forget(modname):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000105 '''"Forget" a module was ever imported by removing it from sys.modules and
106 deleting any .pyc and .pyo files.'''
Fred Drake004d5e62000-10-23 17:22:08 +0000107 unload(modname)
Fred Drake004d5e62000-10-23 17:22:08 +0000108 for dirname in sys.path:
Skip Montanaro7a98be22007-08-16 14:35:24 +0000109 unlink(os.path.join(dirname, modname + '.pyc'))
Brett Cannonf1cfb622003-05-04 21:15:27 +0000110 # Deleting the .pyo file cannot be within the 'try' for the .pyc since
111 # the chance exists that there is no .pyc (and thus the 'try' statement
112 # is exited) but there is a .pyo file.
Skip Montanaro7a98be22007-08-16 14:35:24 +0000113 unlink(os.path.join(dirname, modname + '.pyo'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000114
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000115def is_resource_enabled(resource):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000116 """Test whether a resource is enabled. Known resources are set by
117 regrtest.py."""
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000118 return use_resources is not None and resource in use_resources
119
Barry Warsawc0fb6052001-08-20 22:29:23 +0000120def requires(resource, msg=None):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000121 """Raise ResourceDenied if the specified resource is not available.
122
123 If the caller's module is __main__ then automatically return True. The
124 possibility of False being returned occurs when regrtest.py is executing."""
Skip Montanarod839ecd2003-04-24 19:06:57 +0000125 # see if the caller's module is __main__ - if so, treat as if
126 # the resource was set
127 if sys._getframe().f_back.f_globals.get("__name__") == "__main__":
128 return
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000129 if not is_resource_enabled(resource):
Barry Warsawc0fb6052001-08-20 22:29:23 +0000130 if msg is None:
131 msg = "Use of the `%s' resource not enabled" % resource
Fred Drake9a0db072003-02-03 15:19:30 +0000132 raise ResourceDenied(msg)
Barry Warsawc0fb6052001-08-20 22:29:23 +0000133
Christian Heimes5e696852008-04-09 08:37:03 +0000134HOST = 'localhost'
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000135
Christian Heimes5e696852008-04-09 08:37:03 +0000136def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM):
137 """Returns an unused port that should be suitable for binding. This is
138 achieved by creating a temporary socket with the same family and type as
139 the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to
140 the specified host address (defaults to 0.0.0.0) with the port set to 0,
141 eliciting an unused ephemeral port from the OS. The temporary socket is
142 then closed and deleted, and the ephemeral port is returned.
143
144 Either this method or bind_port() should be used for any tests where a
145 server socket needs to be bound to a particular port for the duration of
146 the test. Which one to use depends on whether the calling code is creating
147 a python socket, or if an unused port needs to be provided in a constructor
148 or passed to an external program (i.e. the -accept argument to openssl's
149 s_server mode). Always prefer bind_port() over find_unused_port() where
150 possible. Hard coded ports should *NEVER* be used. As soon as a server
151 socket is bound to a hard coded port, the ability to run multiple instances
152 of the test simultaneously on the same host is compromised, which makes the
153 test a ticking time bomb in a buildbot environment. On Unix buildbots, this
154 may simply manifest as a failed test, which can be recovered from without
155 intervention in most cases, but on Windows, the entire python process can
156 completely and utterly wedge, requiring someone to log in to the buildbot
157 and manually kill the affected process.
158
159 (This is easy to reproduce on Windows, unfortunately, and can be traced to
160 the SO_REUSEADDR socket option having different semantics on Windows versus
161 Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind,
162 listen and then accept connections on identical host/ports. An EADDRINUSE
163 socket.error will be raised at some point (depending on the platform and
164 the order bind and listen were called on each socket).
165
166 However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE
167 will ever be raised when attempting to bind two identical host/ports. When
168 accept() is called on each socket, the second caller's process will steal
169 the port from the first caller, leaving them both in an awkwardly wedged
170 state where they'll no longer respond to any signals or graceful kills, and
171 must be forcibly killed via OpenProcess()/TerminateProcess().
172
173 The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option
174 instead of SO_REUSEADDR, which effectively affords the same semantics as
175 SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open
176 Source world compared to Windows ones, this is a common mistake. A quick
177 look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when
178 openssl.exe is called with the 's_server' option, for example. See
179 http://bugs.python.org/issue2550 for more info. The following site also
180 has a very thorough description about the implications of both REUSEADDR
181 and EXCLUSIVEADDRUSE on Windows:
182 http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx)
183
184 XXX: although this approach is a vast improvement on previous attempts to
185 elicit unused ports, it rests heavily on the assumption that the ephemeral
186 port returned to us by the OS won't immediately be dished back out to some
187 other process when we close and delete our temporary socket but before our
188 calling code has a chance to bind the returned port. We can deal with this
189 issue if/when we come across it.
190 """
191
192 tempsock = socket.socket(family, socktype)
193 port = bind_port(tempsock)
194 tempsock.close()
195 del tempsock
196 return port
197
198def bind_port(sock, host=HOST):
199 """Bind the socket to a free port and return the port number. Relies on
200 ephemeral ports in order to ensure we are using an unbound port. This is
201 important as many tests may be running simultaneously, especially in a
202 buildbot environment. This method raises an exception if the sock.family
203 is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR
204 or SO_REUSEPORT set on it. Tests should *never* set these socket options
205 for TCP/IP sockets. The only case for setting these options is testing
206 multicasting via multiple UDP sockets.
207
208 Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e.
209 on Windows), it will be set on the socket. This will prevent anyone else
210 from bind()'ing to our host/port for the duration of the test.
211 """
212
213 if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM:
214 if hasattr(socket, 'SO_REUSEADDR'):
215 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1:
216 raise TestFailed("tests should never set the SO_REUSEADDR " \
217 "socket option on TCP/IP sockets!")
218 if hasattr(socket, 'SO_REUSEPORT'):
219 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
220 raise TestFailed("tests should never set the SO_REUSEPORT " \
221 "socket option on TCP/IP sockets!")
222 if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
223 sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
224
225 sock.bind((host, 0))
226 port = sock.getsockname()[1]
227 return port
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000228
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000229FUZZ = 1e-6
230
231def fcmp(x, y): # fuzzy comparison function
Neal Norwitz79212992006-08-21 16:27:31 +0000232 if isinstance(x, float) or isinstance(y, float):
Fred Drake004d5e62000-10-23 17:22:08 +0000233 try:
Fred Drake004d5e62000-10-23 17:22:08 +0000234 fuzz = (abs(x) + abs(y)) * FUZZ
235 if abs(x-y) <= fuzz:
236 return 0
237 except:
238 pass
Neal Norwitz79212992006-08-21 16:27:31 +0000239 elif type(x) == type(y) and isinstance(x, (tuple, list)):
Fred Drake004d5e62000-10-23 17:22:08 +0000240 for i in range(min(len(x), len(y))):
241 outcome = fcmp(x[i], y[i])
Fred Drake132dce22000-12-12 23:11:42 +0000242 if outcome != 0:
Fred Drake004d5e62000-10-23 17:22:08 +0000243 return outcome
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000244 return (len(x) > len(y)) - (len(x) < len(y))
245 return (x > y) - (x < y)
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000246
Finn Bock57bc5fa2002-11-01 18:02:03 +0000247is_jython = sys.platform.startswith('java')
248
Barry Warsaw559f6682001-03-23 18:04:02 +0000249# Filename used for testing
250if os.name == 'java':
251 # Jython disallows @ in module names
252 TESTFN = '$test'
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000253else:
Barry Warsaw559f6682001-03-23 18:04:02 +0000254 TESTFN = '@test'
Walter Dörwald9b775532007-06-08 14:30:53 +0000255
256 # Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding()
257 # TESTFN_UNICODE is a filename that can be encoded using the
258 # file system encoding, but *not* with the default (ascii) encoding
259 TESTFN_UNICODE = "@test-\xe0\xf2"
260 TESTFN_ENCODING = sys.getfilesystemencoding()
261 # TESTFN_UNICODE_UNENCODEABLE is a filename that should *not* be
262 # able to be encoded by *either* the default or filesystem encoding.
263 # This test really only makes sense on Windows NT platforms
264 # which have special Unicode support in posixmodule.
265 if (not hasattr(sys, "getwindowsversion") or
266 sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME
267 TESTFN_UNICODE_UNENCODEABLE = None
268 else:
269 # Japanese characters (I think - from bug 846133)
270 TESTFN_UNICODE_UNENCODEABLE = "@test-\u5171\u6709\u3055\u308c\u308b"
271 try:
272 # XXX - Note - should be using TESTFN_ENCODING here - but for
273 # Windows, "mbcs" currently always operates as if in
274 # errors=ignore' mode - hence we get '?' characters rather than
275 # the exception. 'Latin1' operates as we expect - ie, fails.
276 # See [ 850997 ] mbcs encoding ignores errors
277 TESTFN_UNICODE_UNENCODEABLE.encode("Latin1")
278 except UnicodeEncodeError:
279 pass
Martin v. Löwis2411a2d2002-11-09 19:57:26 +0000280 else:
Georg Brandldb028442008-02-05 20:48:58 +0000281 print('WARNING: The filename %r CAN be encoded by the filesystem. '
282 'Unicode filename tests may not be effective'
283 % TESTFN_UNICODE_UNENCODEABLE)
Neal Norwitz26a1eef2002-11-03 00:35:53 +0000284
285# Make sure we can write to TESTFN, try in /tmp if we can't
286fp = None
287try:
288 fp = open(TESTFN, 'w+')
289except IOError:
290 TMP_TESTFN = os.path.join('/tmp', TESTFN)
291 try:
292 fp = open(TMP_TESTFN, 'w+')
293 TESTFN = TMP_TESTFN
294 del TMP_TESTFN
295 except IOError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000296 print(('WARNING: tests will fail, unable to write to: %s or %s' %
297 (TESTFN, TMP_TESTFN)))
Neal Norwitz26a1eef2002-11-03 00:35:53 +0000298if fp is not None:
299 fp.close()
Neal Norwitz0e17f8c2006-01-23 07:51:27 +0000300 unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000301del fp
Guido van Rossuma8f7e592001-03-13 09:31:07 +0000302
Guido van Rossume26132c1998-04-23 20:13:30 +0000303def findfile(file, here=__file__):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000304 """Try to find a file on sys.path and the working directory. If it is not
305 found the argument passed to the function is returned (this does not
306 necessarily signal failure; could still be the legitimate path)."""
Fred Drake004d5e62000-10-23 17:22:08 +0000307 if os.path.isabs(file):
308 return file
Fred Drake004d5e62000-10-23 17:22:08 +0000309 path = sys.path
310 path = [os.path.dirname(here)] + path
311 for dn in path:
312 fn = os.path.join(dn, file)
313 if os.path.exists(fn): return fn
314 return file
Marc-André Lemburg36619082001-01-17 19:11:13 +0000315
316def verify(condition, reason='test failed'):
Guido van Rossuma1374e42001-01-19 19:01:56 +0000317 """Verify that condition is true. If not, raise TestFailed.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000318
Skip Montanaroc955f892001-01-20 19:12:54 +0000319 The optional argument reason can be given to provide
Tim Peters983874d2001-01-19 05:59:21 +0000320 a better error text.
Tim Petersd2bf3b72001-01-18 02:22:22 +0000321 """
Tim Peters983874d2001-01-19 05:59:21 +0000322
Tim Petersd2bf3b72001-01-18 02:22:22 +0000323 if not condition:
Guido van Rossuma1374e42001-01-19 19:01:56 +0000324 raise TestFailed(reason)
Jeremy Hylton47793992001-02-19 15:35:26 +0000325
Tim Petersc2fe6182001-10-30 23:20:46 +0000326def vereq(a, b):
Tim Peters77902972001-12-29 17:34:57 +0000327 """Raise TestFailed if a == b is false.
328
329 This is better than verify(a == b) because, in case of failure, the
330 error message incorporates repr(a) and repr(b) so you can see the
331 inputs.
332
333 Note that "not (a == b)" isn't necessarily the same as "a != b"; the
334 former is tested.
335 """
336
Tim Petersc2fe6182001-10-30 23:20:46 +0000337 if not (a == b):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000338 raise TestFailed("%r == %r" % (a, b))
Tim Petersc2fe6182001-10-30 23:20:46 +0000339
Tim Peters2f228e72001-05-13 00:19:31 +0000340def sortdict(dict):
341 "Like repr(dict), but in sorted order."
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000342 items = sorted(dict.items())
Tim Peters2f228e72001-05-13 00:19:31 +0000343 reprpairs = ["%r: %r" % pair for pair in items]
344 withcommas = ", ".join(reprpairs)
345 return "{%s}" % withcommas
346
Thomas Wouters89f507f2006-12-13 04:49:30 +0000347def check_syntax_error(testcase, statement):
Jeremy Hylton47793992001-02-19 15:35:26 +0000348 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 compile(statement, '<test string>', 'exec')
Jeremy Hylton47793992001-02-19 15:35:26 +0000350 except SyntaxError:
351 pass
352 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000353 testcase.fail('Missing SyntaxError: "%s"' % statement)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000354
Martin v. Löwis234a34a2007-08-30 20:58:02 +0000355def open_urlresource(url, *args, **kw):
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000356 import urllib.request, urllib.parse
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000357
Guido van Rossum360e4b82007-05-14 22:51:27 +0000358 requires('urlfetch')
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000359 filename = urllib.parse.urlparse(url)[2].split('/')[-1] # '/': it's URL!
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000360
361 for path in [os.path.curdir, os.path.pardir]:
362 fn = os.path.join(path, filename)
363 if os.path.exists(fn):
Martin v. Löwis234a34a2007-08-30 20:58:02 +0000364 return open(fn, *args, **kw)
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000365
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000366 print('\tfetching %s ...' % url, file=get_original_stdout())
Jeremy Hylton1afc1692008-06-18 20:49:58 +0000367 fn, _ = urllib.request.urlretrieve(url, filename)
Martin v. Löwis234a34a2007-08-30 20:58:02 +0000368 return open(fn, *args, **kw)
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000369
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000370
Guido van Rossumd8faa362007-04-27 19:54:29 +0000371class WarningMessage(object):
Nick Coghlanb1304932008-07-13 12:25:08 +0000372 "Holds the result of a single showwarning() call"
373 _WARNING_DETAILS = "message category filename lineno line".split()
374 def __init__(self, message, category, filename, lineno, line=None):
375 for attr in self._WARNING_DETAILS:
376 setattr(self, attr, locals()[attr])
377 self._category_name = category.__name__ if category else None
Christian Heimes33fe8092008-04-13 13:53:33 +0000378
379 def __str__(self):
380 return ("{message : %r, category : %r, filename : %r, lineno : %s, "
Nick Coghlanb1304932008-07-13 12:25:08 +0000381 "line : %r}" % (self.message, self._category_name,
382 self.filename, self.lineno, self.line))
Christian Heimes33fe8092008-04-13 13:53:33 +0000383
Nick Coghlanb1304932008-07-13 12:25:08 +0000384class WarningRecorder(object):
385 "Records the result of any showwarning calls"
386 def __init__(self):
387 self.warnings = []
388 self._set_last(None)
389
390 def _showwarning(self, message, category, filename, lineno,
391 file=None, line=None):
392 wm = WarningMessage(message, category, filename, lineno, line)
393 self.warnings.append(wm)
394 self._set_last(wm)
395
396 def _set_last(self, last_warning):
397 if last_warning is None:
398 for attr in WarningMessage._WARNING_DETAILS:
399 setattr(self, attr, None)
400 else:
401 for attr in WarningMessage._WARNING_DETAILS:
402 setattr(self, attr, getattr(last_warning, attr))
403
404 def reset(self):
405 self.warnings = []
406 self._set_last(None)
407
408 def __str__(self):
409 return '[%s]' % (', '.join(map(str, self.warnings)))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000410
411@contextlib.contextmanager
Benjamin Peterson699adb92008-05-08 22:27:58 +0000412def catch_warning(module=warnings, record=True):
Nick Coghlanb1304932008-07-13 12:25:08 +0000413 """Guard the warnings filter from being permanently changed and
414 optionally record the details of any warnings that are issued.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000415
416 Use like this:
417
Guido van Rossumaf554a02007-08-16 23:48:43 +0000418 with catch_warning() as w:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000419 warnings.warn("foo")
420 assert str(w.message) == "foo"
421 """
Nick Coghlanb1304932008-07-13 12:25:08 +0000422 original_filters = module.filters
Christian Heimes33fe8092008-04-13 13:53:33 +0000423 original_showwarning = module.showwarning
Benjamin Peterson699adb92008-05-08 22:27:58 +0000424 if record:
Nick Coghlanb1304932008-07-13 12:25:08 +0000425 recorder = WarningRecorder()
426 module.showwarning = recorder._showwarning
427 else:
428 recorder = None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000429 try:
Nick Coghlanb1304932008-07-13 12:25:08 +0000430 # Replace the filters with a copy of the original
431 module.filters = module.filters[:]
432 yield recorder
Guido van Rossumd8faa362007-04-27 19:54:29 +0000433 finally:
Christian Heimes33fe8092008-04-13 13:53:33 +0000434 module.showwarning = original_showwarning
435 module.filters = original_filters
Guido van Rossumd8faa362007-04-27 19:54:29 +0000436
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000437
438class CleanImport(object):
439 """Context manager to force import to return a new module reference.
440
441 This is useful for testing module-level behaviours, such as
Nick Coghlanb1304932008-07-13 12:25:08 +0000442 the emission of a DeprecationWarning on import.
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000443
444 Use like this:
445
446 with CleanImport("foo"):
447 __import__("foo") # new reference
448 """
449
450 def __init__(self, *module_names):
451 self.original_modules = sys.modules.copy()
452 for module_name in module_names:
453 if module_name in sys.modules:
454 module = sys.modules[module_name]
455 # It is possible that module_name is just an alias for
456 # another module (e.g. stub for modules renamed in 3.x).
457 # In that case, we also need delete the real module to clear
458 # the import cache.
459 if module.__name__ != module_name:
460 del sys.modules[module.__name__]
461 del sys.modules[module_name]
462
463 def __enter__(self):
464 return self
465
466 def __exit__(self, *ignore_exc):
467 sys.modules.update(self.original_modules)
468
469
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000470class EnvironmentVarGuard(object):
471
472 """Class to help protect the environment variable properly. Can be used as
473 a context manager."""
474
475 def __init__(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000476 self._environ = os.environ
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000477 self._unset = set()
478 self._reset = dict()
479
480 def set(self, envvar, value):
481 if envvar not in self._environ:
482 self._unset.add(envvar)
483 else:
484 self._reset[envvar] = self._environ[envvar]
485 self._environ[envvar] = value
486
487 def unset(self, envvar):
488 if envvar in self._environ:
489 self._reset[envvar] = self._environ[envvar]
490 del self._environ[envvar]
491
492 def __enter__(self):
493 return self
494
495 def __exit__(self, *ignore_exc):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000496 for envvar, value in self._reset.items():
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000497 self._environ[envvar] = value
498 for unset in self._unset:
499 del self._environ[unset]
500
Guido van Rossumd8faa362007-04-27 19:54:29 +0000501class TransientResource(object):
502
503 """Raise ResourceDenied if an exception is raised while the context manager
504 is in effect that matches the specified exception and attributes."""
505
506 def __init__(self, exc, **kwargs):
507 self.exc = exc
508 self.attrs = kwargs
509
510 def __enter__(self):
511 return self
512
513 def __exit__(self, type_=None, value=None, traceback=None):
514 """If type_ is a subclass of self.exc and value has attributes matching
515 self.attrs, raise ResourceDenied. Otherwise let the exception
516 propagate (if any)."""
517 if type_ is not None and issubclass(self.exc, type_):
518 for attr, attr_value in self.attrs.items():
519 if not hasattr(value, attr):
520 break
521 if getattr(value, attr) != attr_value:
522 break
523 else:
524 raise ResourceDenied("an optional resource is not available")
525
526
527def transient_internet():
528 """Return a context manager that raises ResourceDenied when various issues
529 with the Internet connection manifest themselves as exceptions."""
530 time_out = TransientResource(IOError, errno=errno.ETIMEDOUT)
531 socket_peer_reset = TransientResource(socket.error, errno=errno.ECONNRESET)
532 ioerror_peer_reset = TransientResource(IOError, errno=errno.ECONNRESET)
533 return contextlib.nested(time_out, socket_peer_reset, ioerror_peer_reset)
534
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000535
Thomas Woutersed03b412007-08-28 21:37:11 +0000536@contextlib.contextmanager
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000537def captured_output(stream_name):
538 """Run the 'with' statement body using a StringIO object in place of a
539 specific attribute on the sys module.
540 Example use (with 'stream_name=stdout')::
Thomas Woutersed03b412007-08-28 21:37:11 +0000541
542 with captured_stdout() as s:
Neal Norwitz752abd02008-05-13 04:55:24 +0000543 print("hello")
Thomas Woutersed03b412007-08-28 21:37:11 +0000544 assert s.getvalue() == "hello"
545 """
546 import io
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000547 orig_stdout = getattr(sys, stream_name)
548 setattr(sys, stream_name, io.StringIO())
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000549 try:
550 yield getattr(sys, stream_name)
551 finally:
552 setattr(sys, stream_name, orig_stdout)
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000553
554def captured_stdout():
555 return captured_output("stdout")
Thomas Woutersed03b412007-08-28 21:37:11 +0000556
557
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000558#=======================================================================
Thomas Wouters477c8d52006-05-27 19:21:47 +0000559# Decorator for running a function in a different locale, correctly resetting
560# it afterwards.
561
562def run_with_locale(catstr, *locales):
563 def decorator(func):
564 def inner(*args, **kwds):
565 try:
566 import locale
567 category = getattr(locale, catstr)
568 orig_locale = locale.setlocale(category)
569 except AttributeError:
570 # if the test author gives us an invalid category string
571 raise
572 except:
573 # cannot retrieve original locale, so do nothing
574 locale = orig_locale = None
575 else:
576 for loc in locales:
577 try:
578 locale.setlocale(category, loc)
579 break
580 except:
581 pass
582
583 # now run the function, resetting the locale on exceptions
584 try:
585 return func(*args, **kwds)
586 finally:
587 if locale and orig_locale:
588 locale.setlocale(category, orig_locale)
Neal Norwitz221085d2007-02-25 20:55:47 +0000589 inner.__name__ = func.__name__
Thomas Wouters477c8d52006-05-27 19:21:47 +0000590 inner.__doc__ = func.__doc__
591 return inner
592 return decorator
593
594#=======================================================================
Georg Brandldb028442008-02-05 20:48:58 +0000595# Big-memory-test support. Separate from 'resources' because memory use
596# should be configurable.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000597
598# Some handy shorthands. Note that these are used for byte-limits as well
599# as size-limits, in the various bigmem tests
600_1M = 1024*1024
601_1G = 1024 * _1M
602_2G = 2 * _1G
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000603_4G = 4 * _1G
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000605MAX_Py_ssize_t = sys.maxsize
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000606
Thomas Wouters477c8d52006-05-27 19:21:47 +0000607def set_memlimit(limit):
608 import re
609 global max_memuse
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000610 global real_max_memuse
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 sizes = {
612 'k': 1024,
613 'm': _1M,
614 'g': _1G,
615 't': 1024*_1G,
616 }
617 m = re.match(r'(\d+(\.\d+)?) (K|M|G|T)b?$', limit,
618 re.IGNORECASE | re.VERBOSE)
619 if m is None:
620 raise ValueError('Invalid memory limit %r' % (limit,))
621 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000622 real_max_memuse = memlimit
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 if memlimit > MAX_Py_ssize_t:
624 memlimit = MAX_Py_ssize_t
625 if memlimit < _2G - 1:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000626 raise ValueError('Memory limit %r too low to be useful' % (limit,))
627 max_memuse = memlimit
628
629def bigmemtest(minsize, memuse, overhead=5*_1M):
630 """Decorator for bigmem tests.
631
632 'minsize' is the minimum useful size for the test (in arbitrary,
633 test-interpreted units.) 'memuse' is the number of 'bytes per size' for
634 the test, or a good estimate of it. 'overhead' specifies fixed overhead,
Christian Heimes33fe8092008-04-13 13:53:33 +0000635 independent of the testsize, and defaults to 5Mb.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000636
637 The decorator tries to guess a good value for 'size' and passes it to
638 the decorated test function. If minsize * memuse is more than the
639 allowed memory use (as defined by max_memuse), the test is skipped.
640 Otherwise, minsize is adjusted upward to use up to max_memuse.
641 """
642 def decorator(f):
643 def wrapper(self):
644 if not max_memuse:
645 # If max_memuse is 0 (the default),
646 # we still want to run the tests with size set to a few kb,
647 # to make sure they work. We still want to avoid using
648 # too much memory, though, but we do that noisily.
649 maxsize = 5147
650 self.failIf(maxsize * memuse + overhead > 20 * _1M)
651 else:
652 maxsize = int((max_memuse - overhead) / memuse)
653 if maxsize < minsize:
654 # Really ought to print 'test skipped' or something
655 if verbose:
656 sys.stderr.write("Skipping %s because of memory "
657 "constraint\n" % (f.__name__,))
658 return
659 # Try to keep some breathing room in memory use
660 maxsize = max(maxsize - 50 * _1M, minsize)
661 return f(self, maxsize)
662 wrapper.minsize = minsize
663 wrapper.memuse = memuse
664 wrapper.overhead = overhead
665 return wrapper
666 return decorator
667
Neal Norwitz3ce5d922008-08-24 07:08:55 +0000668def precisionbigmemtest(size, memuse, overhead=5*_1M):
669 def decorator(f):
670 def wrapper(self):
671 if not real_max_memuse:
672 maxsize = 5147
673 else:
674 maxsize = size
675
676 if real_max_memuse and real_max_memuse < maxsize * memuse:
677 if verbose:
678 sys.stderr.write("Skipping %s because of memory "
679 "constraint\n" % (f.__name__,))
680 return
681
682 return f(self, maxsize)
683 wrapper.size = size
684 wrapper.memuse = memuse
685 wrapper.overhead = overhead
686 return wrapper
687 return decorator
688
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000689def bigaddrspacetest(f):
690 """Decorator for tests that fill the address space."""
691 def wrapper(self):
692 if max_memuse < MAX_Py_ssize_t:
693 if verbose:
694 sys.stderr.write("Skipping %s because of memory "
695 "constraint\n" % (f.__name__,))
696 else:
697 return f(self)
698 return wrapper
699
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700#=======================================================================
Guido van Rossumd8faa362007-04-27 19:54:29 +0000701# unittest integration.
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000702
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000703class BasicTestRunner:
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000704 def run(self, test):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000705 result = unittest.TestResult()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000706 test(result)
707 return result
708
709
Guido van Rossumd8faa362007-04-27 19:54:29 +0000710def _run_suite(suite):
Barry Warsawc88425e2001-09-20 06:31:22 +0000711 """Run tests from a unittest.TestSuite-derived class."""
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000712 if verbose:
Fred Drake84a59342001-03-23 04:21:17 +0000713 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000714 else:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000715 runner = BasicTestRunner()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000716
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000717 result = runner.run(suite)
718 if not result.wasSuccessful():
Fred Drake14f6c182001-07-16 18:51:32 +0000719 if len(result.errors) == 1 and not result.failures:
720 err = result.errors[0][1]
721 elif len(result.failures) == 1 and not result.errors:
722 err = result.failures[0][1]
723 else:
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000724 err = "errors occurred; run in verbose mode for details"
Tim Peters2d84f2c2001-09-08 03:37:56 +0000725 raise TestFailed(err)
Tim Petersa0a62222001-09-09 06:12:01 +0000726
Barry Warsawc10d6902001-09-20 06:30:41 +0000727
Walter Dörwald21d3a322003-05-01 17:45:56 +0000728def run_unittest(*classes):
729 """Run tests from unittest.TestCase-derived classes."""
Guido van Rossumd8faa362007-04-27 19:54:29 +0000730 valid_types = (unittest.TestSuite, unittest.TestCase)
Raymond Hettinger9dcbbea2003-04-27 07:54:23 +0000731 suite = unittest.TestSuite()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000732 for cls in classes:
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000733 if isinstance(cls, str):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000734 if cls in sys.modules:
735 suite.addTest(unittest.findTestCases(sys.modules[cls]))
736 else:
737 raise ValueError("str arguments must be keys in sys.modules")
738 elif isinstance(cls, valid_types):
Raymond Hettinger21d99872003-07-16 02:59:32 +0000739 suite.addTest(cls)
740 else:
741 suite.addTest(unittest.makeSuite(cls))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000742 _run_suite(suite)
Raymond Hettinger9dcbbea2003-04-27 07:54:23 +0000743
Barry Warsawc10d6902001-09-20 06:30:41 +0000744
Tim Petersa0a62222001-09-09 06:12:01 +0000745#=======================================================================
746# doctest driver.
747
748def run_doctest(module, verbosity=None):
Tim Peters17111f32001-10-03 04:08:26 +0000749 """Run doctest on the given module. Return (#failures, #tests).
Tim Petersa0a62222001-09-09 06:12:01 +0000750
751 If optional argument verbosity is not specified (or is None), pass
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000752 support's belief about verbosity on to doctest. Else doctest's
Tim Petersbea3fb82001-09-10 01:39:21 +0000753 usual behavior is used (it searches sys.argv for -v).
Tim Petersa0a62222001-09-09 06:12:01 +0000754 """
755
756 import doctest
757
758 if verbosity is None:
759 verbosity = verbose
760 else:
761 verbosity = None
762
Tim Peters342ca752001-09-25 19:13:20 +0000763 # Direct doctest output (normally just errors) to real stdout; doctest
764 # output shouldn't be compared by regrtest.
765 save_stdout = sys.stdout
Tim Peters8dee8092001-09-25 20:05:11 +0000766 sys.stdout = get_original_stdout()
Tim Peters342ca752001-09-25 19:13:20 +0000767 try:
768 f, t = doctest.testmod(module, verbose=verbosity)
769 if f:
770 raise TestFailed("%d of %d doctests failed" % (f, t))
771 finally:
772 sys.stdout = save_stdout
Raymond Hettinger35b34bd2003-05-17 00:58:33 +0000773 if verbose:
Georg Brandldb028442008-02-05 20:48:58 +0000774 print('doctest (%s) ... %d tests with zero failures' %
775 (module.__name__, t))
Raymond Hettinger35b34bd2003-05-17 00:58:33 +0000776 return f, t
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000777
778#=======================================================================
779# Threading support to prevent reporting refleaks when running regrtest.py -R
780
781def threading_setup():
782 import threading
783 return len(threading._active), len(threading._limbo)
784
785def threading_cleanup(num_active, num_limbo):
786 import threading
787 import time
788
789 _MAX_COUNT = 10
790 count = 0
791 while len(threading._active) != num_active and count < _MAX_COUNT:
792 count += 1
793 time.sleep(0.1)
794
795 count = 0
796 while len(threading._limbo) != num_limbo and count < _MAX_COUNT:
797 count += 1
798 time.sleep(0.1)
799
800def reap_children():
801 """Use this function at the end of test_main() whenever sub-processes
802 are started. This will help ensure that no extra children (zombies)
803 stick around to hog resources and create problems when looking
804 for refleaks.
805 """
806
807 # Reap all our dead child processes so we don't leave zombies around.
808 # These hog resources and might be causing some of the buildbots to die.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000809 if hasattr(os, 'waitpid'):
810 any_process = -1
811 while True:
812 try:
813 # This will raise an exception on Windows. That's ok.
814 pid, status = os.waitpid(any_process, os.WNOHANG)
815 if pid == 0:
816 break
817 except:
818 break